function not fetching data from firebase also showing no error - database

I have a function to fetch data from firebase firestore everything working fine except its not fetching data and it also shows no error I don't know why please help me out here
my code functions like I would pick a document name using a list picker and tap the get button(inside map page) and it should fetch the data from the database and give results for the specific page
inside the function(fetchmap)the loading bar is also not working I don't know why?
here is my code:
Mappage.dart :
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:helping_hand/UI/Other/Emergency.dart';
import 'package:list_picker/list_picker.dart';
import '../../StateManagement/MapController.dart';
import '../../drawers/bottomnavbar.dart';
import '../Bottom Navigation/My status.dart';
class Mappage extends StatelessWidget {
Mappage({Key? key}) : super(key: key);
final TextEditingController selectPage = TextEditingController();
final List<String> pageList = ['helps','sitrep','location','emergencies', 'users',];
#override
Widget build(BuildContext context) {
MapController mapcontroller = Get.find();
return Scaffold(
body: Stack(
children: [
SizedBox(height: MediaQuery.of(context).size.height,
child: SingleChildScrollView(
child: SizedBox(height: MediaQuery.of(context).size.height,width: MediaQuery.of(context).size.width,
child: Column(
children: [
Stack(
children: [
SizedBox(
height: MediaQuery.of(context).size.height,
child: GetBuilder<MapController>(builder: (_)=>GoogleMap(initialCameraPosition:MapController.initial,
mapType: MapType.normal,markers:mapcontroller.markers,
onMapCreated: (GoogleMapController controller){
mapcontroller.completercontrol.complete(controller);
mapcontroller.googleMapController = controller;
},),)
),
Positioned(
top: 50,
height: 60,
width: MediaQuery.of(context).size.width,
child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
SizedBox(width: MediaQuery.of(context).size.width/2,
child: ListPickerField(label:'select', items:pageList,controller:selectPage,)),
ElevatedButton(
style:const ButtonStyle(backgroundColor:MaterialStatePropertyAll(Colors.redAccent)) ,
onPressed: () {
if(selectPage.text.isNotEmpty){
mapcontroller.fetchMap(selectPage.text);
}else{
Get.snackbar('error','select a page from dropdown menu');
}
},
child:const Text('Get',style: TextStyle(color: Colors.white),)),
FloatingActionButton(
heroTag: 'btn1',
onPressed:(){
Get.to(()=>Emergency());
},
child: const Icon(Icons.emergency_outlined),
)
],
),
),
Positioned(
bottom: 50,
width: MediaQuery.of(context).size.width,
child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
style:const ButtonStyle(backgroundColor:MaterialStatePropertyAll(Colors.redAccent)) ,
onPressed: () {
Get.offAll(()=>Nav());
},
child:const Text('Go to Dashboard',style: TextStyle(color: Colors.white),)),
FloatingActionButton(
heroTag: 'btn2',
onPressed: () {
mapcontroller.getlatlong();
},
child:const Icon(Icons.location_on,color: Colors.white,)),
],
),
),
],
),
],
),
),
),
),
GetBuilder<MapController>(builder: (_){
if (mapcontroller.isloading == true) {
return Container(
color: Colors.white.withOpacity(0.5),
child: const Center(
child: CircularProgressIndicator(backgroundColor: Colors.redAccent,color: Colors.white,),
),
);
} else {
return const SizedBox.shrink();
}
}),
],
));
}
}
and my mapcontroller (function is fetchmap at the bottom)(using getx):
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:helping_hand/UI/Map/Mappage.dart';
class MapController extends GetxController{
String locationmessage = 'currentlocation of user';
late String lat ;
late String long ;
Set<Marker>markers={};
#override
void onInit() {
lat = '10.228370';
long ='76.198799';
super.onInit();
}
late GoogleMapController googleMapController;
final Completer<GoogleMapController> _controller = Completer();
Completer<GoogleMapController> get completercontrol => _controller;
static CameraPosition initial = const CameraPosition(target:LatLng(10.228370,76.198799),zoom: 15);
//lower part
Future<Position> getCurrentLocation() async {
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('the location is not enabled');
}
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error(
'location permissions are permanantly denied, cannot grant acess');
}
}
if (permission == LocationPermission.deniedForever) {
return Future.error(
'location permissions are permanantly denied, cannot grant acess');
}
return await Geolocator.getCurrentPosition();
}
void liveLocation() {
loadingbar();
LocationSettings settings = const LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
);
Geolocator.getPositionStream(locationSettings: settings)
.listen((Position position) {
lat = position.latitude.toString();
long = position.longitude.toString();
googleMapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(target:LatLng(position.latitude,position.longitude),zoom: 15)));
markers.clear();
markers.add(Marker(markerId:const MarkerId('current user location'),position: LatLng(position.latitude,position.longitude)));
update();
});
loadingbaroff();
}
void getlatlong(){
loadingbar();
getCurrentLocation().then((value){
lat = '${value.latitude}';
long = '${value.longitude}';
googleMapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(target:LatLng(value.latitude,value.longitude),zoom: 15)));
markers.clear();
markers.add(Marker(markerId:const MarkerId('current user location'),position: LatLng(value.latitude,value.longitude)));
update();
if (kDebugMode) {
print(lat);
}
if (kDebugMode) {
print(long);
}
liveLocation();
});
loadingbaroff();
}
void liveLocationToUpload(){
LocationSettings settings = const LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
);
Geolocator.getPositionStream(locationSettings: settings)
.listen((Position position) {
lat = position.latitude.toString();
long = position.longitude.toString();
update();
});
}
//MAP PAGE CONTROLLER CONTROLLS ::::::::::::::
List<DocsForMap> docsformap=[];
Future<void> fetchMap(String page)async {
print('this is starting of fetch map');
loadingbar();
update();
print('after loading bar $isloading');
docsformap.clear();
print('cleared the values of list');
final CollectionReference maps = FirebaseFirestore.instance.collection('map').doc('maps').collection(page);
try{
print('inside try catch');
var data = maps.get();
print('this is dataaa ::::: $data');
maps.get().then((snapshot) => (){
print('inside values::::: $snapshot');
for (var document in snapshot.docs) {
if (kDebugMode) {
print('I AM ACESSING DATA HEREEEEEEEE :::;;;;;;;;;;');
}
if (kDebugMode) {print(document.data());}
}
});
}catch(e){
Get.snackbar('error','error while fetching $page');
print('error happenddddd :::::::: $e');
loadingbaroff();
}
loadingbaroff();
}
bool isloading = false;
void loadingbar() {
isloading = true;
update();
}
void loadingbaroff() {
isloading = false;
update();
}
}
my console :
min=41.46
I/flutter (14542): this is starting of fetch map
I/flutter (14542): after loading bar true
I/flutter (14542): cleared the values of list
I/flutter (14542): inside try catch
I/flutter (14542): this is dataaa ::::: Instance of 'Future<QuerySnapshot<Map<String, dynamic>>>'
W/DynamiteModule(14542): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule(14542): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller(14542): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
I/System.out(14542): [socket]:check permission begin!
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
I/System.out(14542): [socket]:check permission begin!
I/System.out(14542): [socket]:check permission begin!
I/System.out(14542): [socket]:check permission begin!
D/ViewRootImpl(14542): setSurfaceViewCreated, created:false
D/Surface (14542): Surface::disconnect(this=0x7500239000,api=-1)
D/Surface (14542): Surface::disconnect(this=0x755e735000,api=1)
I/BufferQueueProducer(14542): [SurfaceTexture-0-14542-4](this:0x74a100b000,id:7,api:1,p:14542,c:14542) disconnect(P): api 1
I/BufferQueueProducer(14542): [ImageReader-1080x2340f1m3-14542-0](this:0x74cfcc8800,id:0,api:1,p:14542,c:14542) queueBuffer: fps=0.05 dur=600096.19 max=599491.31 min=10.40
I/BufferQueueProducer(14542): [ImageReader-1080x2340f1m3-14542-2](this:0x74cf08b800,id:3,api:1,p:14542,c:14542) queueBuffer: fps=0.05 dur=600100.68 max=599496.03 min=9.76
I/GED (14542): ged_boost_gpu_freq, level 100, eOrigin 2, final_idx 29, oppidx_max 29, oppidx_min 0
W/le.helping_han(14542): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
D/Surface (14542): Surface::disconnect(this=0x754e09a000,api=1)
E/libprocessgroup(14542): set_timerslack_ns write failed: Operation not permitted

Try with following code for method fetchMap
Future<void> fetchMap(String page) async {
print('this is starting of fetch map');
loadingbar();
update();
print('after loading bar $isloading');
docsformap.clear();
print('cleared the values of list');
final CollectionReference maps = FirebaseFirestore.instance.collection('map').doc('maps').collection(page);
try{
print('inside try catch');
var data = await maps.get();
print('this is dataa ::::: $data');
for (var document in data.docs) {
if (kDebugMode) {
print('I AM ACESSING DATA HEREEEEEEEE :::;;;;;;;;;;');
}
if (kDebugMode) {print(document.data());}
}
} catch(e) {
Get.snackbar('error','error while fetching $page');
print('error happenddddd :::::::: $e');
}
loadingbaroff();
}

Related

Flutter: How to pass array data from json Future< to Widget

Being new to flutter, I'm learning and stumbling on the go.
I am trying to pass an array that I have received from json into an already waiting widget structure but can't quite seem to get the connection.
Here's the sample code:
class Products extends StatefulWidget {
#override
_ProductsState createState() => _ProductsState();
}
class _ProductsState extends State<Products> {
#override
void initState() {
_getProducts();
}
Future<List<Single_prod>> _getProducts() async {
var url = "";
var data = await http.get(url);
var jsonData = json.decode(data.body) as Map<String, dynamic>;
//print(jsonData.toString());
//jsonData.forEach((k, v) => print("Key : $k, Value : $v"));
List<Single_prod> items = [];
jsonData.forEach((k, v){
Single_prod item = Single_prod(v["error"], v["id"], v["name"], v["price"], v["image"]);
items.add(item);
});
//print(items.length);
return items; <---Tring to pass this to Widget build but not recognized.....
}
#override
Widget build(BuildContext context) {
return GridView.builder(
itemCount: items.length,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemBuilder: (BuildContext context, int index){
return Single_prod(
prod_err: items[index]['error'], <--- This items array is not recognized
prod_id: items[index]['id'],
prod_name: items[index]['name'],
prod_price: items[index]['price'],
prod_image: items[index]['image'],
);
});
}
}
The items array is not recognized in the widget
Here is the rest of the code:
class Single_prod extends StatelessWidget {
final prod_err;
final prod_id;
final prod_name;
final prod_price;
final prod_image;
Single_prod({
this.prod_err,
this.prod_id,
this.prod_name,
this.prod_price,
this.prod_image,
});
#override
Widget build(BuildContext context) {
return Card(
child: Hero(
tag: prod_name,
child: Material(
child: InkWell(
onTap: () => Navigator.of(context).push(new MaterialPageRoute(
// here we are passing the values of the products to the details page
builder: (context) => new ProductDetails(
prod_detail_name: prod_name,
prod_detail_image: prod_image,
prod_detail_id: prod_id,
prod_detail_price: prod_price,
))),
child: GridTile(
footer: Container(
height: 40.0,
color: Colors.white70,
child: ListTile(
leading: Text(prod_name, style: TextStyle(fontWeight: FontWeight.bold),),
title: Text(
prod_price,
style: TextStyle(color: Colors.blue, fontWeight: FontWeight.w800, fontSize: 12),
),
/*subtitle: Text(
prod_oldprice,
style: TextStyle(color: Colors.black, fontWeight: FontWeight.w800, fontSize: 11, decoration: TextDecoration.lineThrough),
),*/
),
),
child: Image.asset(prod_image,
fit: BoxFit.cover,),
),
),
),
),
);
}
}
How does the upper code connect with the lower code?
Thanks in advance.
First, look at the scope of your 'items' variable: it is defined in getItems() function, and it is not visible outside the function. So, first thing: make it class level property.
Next - your initState will call your method. Method is async, and the way to handle it in initState to use '.then' on the Future returned by your method. What you want to do here is: once the future completes, you want to set your class level variable to hold the value returned by _getProduct() function.
And finally - this is very important to understand: you don't call build method yourself - flutter framework does it for you. Now, flutter does not have a magic way of knowing when you changed the data - it won't observe your code, so you need to tell it somehow that your state object changed, and it requires rebuild. You do it by calling setState() function.
I think you have another issue here actually: you already bulit your Single_prod widget in _getProduct(), no need to build it again. I tried to correct this also.
Try this (I didn't compile it so it might have few errors):
class Products extends StatefulWidget {
#override
_ProductsState createState() => _ProductsState();
}
class _ProductsState extends State<Products> {
List<Single_prod> items = [];
#override
void initState() {
super.initState();
_getProducts().then( (result) {
setState(() {
items=result;
}
});
}
Future<List<Single_prod>> _getProducts() async {
var url = "";
var data = await http.get(url);
var jsonData = json.decode(data.body) as Map<String, dynamic>;
//print(jsonData.toString());
//jsonData.forEach((k, v) => print("Key : $k, Value : $v"));
List<Single_prod> items = [];
jsonData.forEach((k, v){
Single_prod item = Single_prod(v["error"], v["id"], v["name"], v["price"], v["image"]);
items.add(item);
});
//print(items.length);
return items;
}
#override
Widget build(BuildContext context) {
return GridView.builder(
itemCount: items.length,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemBuilder: (BuildContext context, int index){
return items[index];
});
}
}

Exception has occurred. PlatformException (PlatformException(-3, Permission denied, ))

I tried to build an application, which should write data in a real time database. I tried an example I found in the internet, but now I get an error. The error I get is:
Exception has occurred.
PlatformException (PlatformException(-3, Permission denied, ))
This is the code I tried:
import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
//final DBRef = FirebaseDatabase.instance.reference();
#override
Widget build(BuildContext context) {
return MaterialApp(
title: "Test",
home: Scaffold(
appBar: AppBar(
title: Text("Testo"),
),
body: Column(
children: <Widget>[
RaisedButton(
child: Text("Write Data"),
onPressed: () {
writeData();
},
),
],
),
),
);
}
void writeData() {
print("pressed");
FirebaseDatabase.instance
.reference()
.child("1")
.set({"id": "ID1", "data": "This is a test"});
}
}
I found in the internet, that my rule in the database could be wrong, that could be the mistake, but I didn't find a solution. This are my rules(I haven´t changed them):
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.time < timestamp.date(2020, 7, 29);
}
}
}
I don´t know why I get this error, hopefully someone can help me.

Getting Data from cloud Firestore and showing for flutter app

I have a profile screen. and getting data from the cloud store and showing in the profile screen.
I guess there is no problem while retrieving data but the problem is while showing. I don't know how I mess up?
Now the error is only showing "Loading" Text.
Help me
class Profile extends StatefulWidget {
#override
_ProfileState createState() => _ProfileState();
}
class _ProfileState extends State<Profile> {
bool userFlag = false;
var users;
#override
void initState() {
// TODO: implement initState
super.initState();
UserManagement().getData().then((QuerySnapshot docs){
userFlag = true;
users = docs.documents[0].data;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Profile'),
),
body: Container(
padding: EdgeInsets.all(50),
child: Column(
children:<Widget>[
name(),
],
),
),
);
}
Widget name() {
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(
"Name",
style: TextStyle(fontWeight: FontWeight.w600,fontSize: 18),
),
SizedBox(
width: 45,
),
userFlag ? Text(users['Name'],
style: TextStyle(fontWeight: FontWeight.w400,fontSize: 18),
)
:Text('Loading'),
],
),
);
}
for getting Data i have:
getData(){
return Firestore.instance
.collection('users').getDocuments();
}
I get and Use Data that way:
QuerySnapshot Users = await _fs
.collection("users")
.getDocuments();
It will give you all the users in the users collection.
so for retrieving one user in specific I use a "for loop".
String myEmail = "email#gmail.com";
String username;
for (var user in users.documents) {
if ( myEmail == user.data["email"]){
// you have all the field for the user using "myEmail".
username = user.data["username"];
} else {
print("There is no User with this email");
}
}
But I think there might be a better way to do it.
This error is because, You should initialize your user variable like
var users = {}; instead of
var user;

Encoding / decoding complex Json in Flutter

I am going to use a real json. First of all, I should run the project that is written in Flask, then use the local host to achieve data.
Here is the real Json I`m using
{
"devices":[
{
"device_desc":"cooler",
"device_title":"cooler",
"functions":[
{
"device_id":1,
"function_desc":"pomp",
"function_title":"pomp",
"status":1
},
{
"device_id":1,
"function_desc":"less",
"function_title":"less",
"status":1
},
{
"device_id":1,
"function_desc":"up",
"function_title":"up",
"status":1
}
],
"image_path":"fdfdfsf",
"status_id":1,
"statuss":{
"status_desc":"device is on",
"status_title":"on"
}
},
{
"device_desc":"panke",
"device_title":"panke",
"functions":[
{
"device_id":2,
"function_desc":"less",
"function_title":"pomp",
"status":2
},
{
"device_id":2,
"function_desc":"less",
"function_title":"less",
"status":2
}
],
"image_path":"vfx",
"status_id":2,
"statuss":{
"status_desc":"device is off",
"status_title":"off"
}
}
]
}
This is my code:
these are data models for defining json properties:
class Base{
//the type of our object is the array
List<Device> _devices;
Base(this._devices);
List<Device> get devices => _devices;
set devices(List<Device> value) {
_devices = value;
}
}
class Device {
String _device_desc,_device_title,_image_path;
int _status_id;
List<function> _functions;
List<Status> _statuss ;
Device(this._device_desc, this._device_title, this._image_path,
this._status_id, this._functions, this._statuss);
List<Status> get statuss => _statuss;
set statuss(List<Status> value) {
_statuss = value;
}
List<function> get functions => _functions;
set functions(List<function> value) {
_functions = value;
}
int get status_id => _status_id;
set status_id(int value) {
_status_id = value;
}
get image_path => _image_path;
set image_path(value) {
_image_path = value;
}
get device_title => _device_title;
set device_title(value) {
_device_title = value;
}
String get device_desc => _device_desc;
set device_desc(String value) {
_device_desc = value;
}
}
class Status {
String _status_desc, _status_title;
Status(this._status_desc, this._status_title);
get status_title => _status_title;
set status_title(value) {
_status_title = value;
}
String get status_desc => _status_desc;
set status_desc(String value) {
_status_desc = value;
}}
class function {
String _function_desc, _function_title;
int _device_id, _status;
function(this._function_desc, this._function_title, this._device_id,
this._status);
get status => _status;
set status(value) {
_status = value;
}
int get device_id => _device_id;
set device_id(int value) {
_device_id = value;
}
get function_title => _function_title;
set function_title(value) {
_function_title = value;
}
String get function_desc => _function_desc;
set function_desc(String value) {
_function_desc = value;
}}
and this is the stateful class :
class MyHomePage extends StatefulWidget {
var title;
MyHomePage({Key key, this.title}) : super(key: key);
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<Base> _getBase() async {
var data = await http.get(Uri.encodeFull("http://192.168.1.111:5000/mobile-home"));
var jsonData = json.decode(data.body);
Base base = Base(jsonData);
return Base(jsonData[0]);
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: Container(
child: FutureBuilder(
future: _getBase(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Container(
child: Center(
child: Text("Loading..."),
),
);
} else {
return ListView.builder(
itemCount: snapshot.data.devices.length,
itemBuilder: (BuildContext context, int index) {
snapshot.data.devices.map<Widget>((devices){
return ListTile(
subtitle: Text(devices[index].device_desc.toString()),
title: Text(devices[index].device_title),
/*leading: CircleAvatar(
// ignore: argument_type_not_assignable
backgroundImage: NetworkImage(snapshot.data[index].thumbnailUrl),
)*/
);
}
);
},
);
}
},
),
),
);
}
}
I got an error when while debugging:
"type 'List<dynamic>' is not a subtype of type 'List<Device>'"
I can not get the data from json.
There was no question in your question, but I assume the question is:
My Json code is not working - How do I efficiently parse and encode complex json objects in my
flutter program.
For complex JSON you may want to consider using code generation to reduce the boiler plate you have to write. The flutter page has a good example using JsonSerializable. Here the basic instructions for your example:
Add dependencies to pubspec.yaml and run flutter pub get in the command line:
dependencies:
json_annotation: ^1.2.0
dev_dependencies:
build_runner: ^1.0.0
json_serializable: ^1.5.1
Create the basic Object model (similar to what you have done). Except for the following differences:
You don't have a List of Status for the field statuss, but a single Status object.
Don't use private fields.
To enable json boiler plate code generation do the following three steps:
add the json-annotations to each class,
add a factory .fromJson method on each class and
add a .toJson method on each class:
#JsonSerializable()
class Base {
List<Device> devices;
Base({this.devices});
factory Base.fromJson(Map<String, dynamic> json) => _$BaseFromJson(json);
Map<String, dynamic> toJson() => _$BaseToJson(this);
}
#JsonSerializable()
class Device {
String device_desc,device_title,image_path;
int status_id;
List<function> functions;
Status statuss ;
Device(this.device_desc, this.device_title, this.image_path,
this.status_id, this.functions, this.statuss);
factory Device.fromJson(Map<String, dynamic> json) => _$DeviceFromJson(json);
Map<String, dynamic> toJson() => _$DeviceToJson(this);
}
#JsonSerializable()
class Status {
String status_desc, status_title;
Status(this.status_desc, this.status_title);
factory Status.fromJson(Map<String, dynamic> json) => _$StatusFromJson(json);
Map<String, dynamic> toJson() => _$StatusToJson(this);
}
#JsonSerializable()
class function {
String function_desc, function_title;
int device_id, status;
function(this.function_desc, this.function_title, this.device_id,
this.status);
factory function.fromJson(Map<String, dynamic> json) => _$functionFromJson(json);
Map<String, dynamic> toJson() => _$functionToJson(this);
}
Run the command line to start code generation in the project root folder:
flutter packages pub run build_runner watch
Now an additional source file appears with your generated boiler plate code. Add this file to your own source file using the part keyword, for example if your source file is main.dart add the following line:
part 'main.g.dart';
And you are done - This is all you need to test your encoding / decoding. For example with the following code:
import 'dart:convert';
void main() => (){
var jsonExample = '{"devices": [{"device_desc": "cooler", "device_title": "cooler", "functions": [{"device_id": 1, "function_desc": "pomp", "function_title": "pomp", "status": 1}, {"device_id": 1, "function_desc": "less", "function_title": "less", "status": 1}, {"device_id": 1, "function_desc": "up", "function_title": "up", "status": 1}], "image_path": "fdfdfsf", "status_id": 1, "statuss": {"status_desc": "device is on", "status_title": "on"}}, {"device_desc": "panke", "device_title": "panke", "functions": [{"device_id": 2, "function_desc": "less", "function_title": "pomp", "status": 2}, {"device_id": 2, "function_desc": "less", "function_title": "less", "status": 2}], "image_path": "vfx", "status_id": 2, "statuss": {"status_desc": "device is off", "status_title": "off"}}]}';
Map base_example = json.decode(jsonExample);
Base base_example_parsed = Base.fromJson(base_example);
var numberDevices = base_example_parsed.devices.length;
var numberFuncs = base_example_parsed.devices[0].functions.length;
print('$base_example_parsed has $numberDevices devices and the first device has $numberFuncs functions');
var base_example_encoded_again = json.encode(base_example_parsed);
print('$base_example_encoded_again');
};
For more information please refer to:
1. the official example.
2. this blog.
There's a very good article about how to parse complex JSON in Flutter. Here's a quick summary...
Simple Stuff:
{
"id":"487349",
"name":"Pooja Bhaumik",
"score" : 1000
}
becomes...
class Student{
String studentId;
String studentName;
int studentScores;
Student({
this.studentId,
this.studentName,
this.studentScores
});
factory Student.fromJson(Map<String, dynamic> parsedJson){
return Student(
studentId: parsedJson['id'],
studentName : parsedJson['name'],
studentScores : parsedJson ['score']
);
}
}
You create a new Student object like Student.fromJson(your_parsed_json).
Sub-objects work in a similar way. For each object inside the parent object you make a new Dart object, each with it's own parser for fromJson. Then inside the parent factory you call that fromJson method (like so)... This also works for lists of objects.
factory Student.fromJson(Map<String, dynamic> parsedJson){
return Student(
studentId: parsedJson['id'],
studentName : parsedJson['name'],
studentScores : Teacher.fromJson(parsedJson['teacher'])
);

How to timeout a timer instead of cancelling [duplicate]

How would you wait for future response for a specific amount of time?
Say, we make a http post request and await for its response before we close the http request, but, we wait for only 3 secs, else we close the request.
How would you achieve that?
Something like
Future makePostReq() async{
....
await http response for 3 secs
....
if(response) {
... Do something with it
}
Http.close
}
You can use Future.any constructor to make a race condition
final result = await Future.any([
Future.value(42),
Future.delayed(const Duration(seconds: 3))
]);
You can also use Future.timeout method
final result = await Future.value(42).timeout(const Duration(seconds: 3));
You can do it very easily
try {
var response = await Http.get("YourUrl").timeout(const Duration(seconds: 3));
if(response.statusCode == 200){
print("Success");
}else{
print("Something wrong");
}
} on TimeoutException catch (e) {
print('Timeout');
} on Error catch (e) {
print('Error: $e');
}
This example sets timeout to 3 second. If it has been 3 seconds and no response received, it will throw TimeoutException
Import this :
import 'package:http/http.dart' as Http;
import 'dart:async';
Future.any([asyncfunc, ...])
Here's an example of using Remi's Future.any solution where the future that returns first, will be used. The other is discarded.
So, the first future is your data-gathering/slow function and the other is a fallback when your call is taking too long.
dynamic result = await Future.any([
getData(fakeDelay: seconds), // ← hope this returns first
timeoutAfter(sec: timeout, onTimeout: () => 'Timed Out!', ) // ← waited too long, do this
]);
Example in Flutter Page
Here's a copy/paste example for a Flutter page:
(look at your debug/run output window for messages)
import 'package:flutter/material.dart';
class FutureTimeoutPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Future or Timeout Page'),
),
body: FutureAnyExample(),
);
}
}
class FutureAnyExample extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Complete before timeout or timeout:'),
SizedBox(height: 30,),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(onPressed: () => getDataOrTimeout(seconds: 1, timeout: 3),
child: Text('In Time')),
ElevatedButton(onPressed: () => getDataOrTimeout(seconds: 5, timeout: 3),
child: Text('Too Slow'))
],
)
],
);
}
Future<void> getDataOrTimeout({int seconds, int timeout}) async {
/// In Future.any, put as many async functions as you need.
/// Whichever completes first, will be returned. All others are discarded
dynamic result = await Future.any([
getData(fakeDelay: seconds), // ← hope this returns first
timeoutAfter(sec: timeout, onTimeout: () => 'Timed Out!', ) // ← waited too long, do this
]);
print(result);
}
/// Mock of a long-running operation like getting DB data, or API call
Future<String> getData({int fakeDelay}) async {
return Future.delayed(Duration(seconds: fakeDelay), () => 'Data returned!');
}
/// Do this in case my long-running op takes too long
/// Can run a function or just return some message
Future<dynamic> timeoutAfter({int sec, Function() onTimeout}) async {
return Future.delayed(Duration(seconds: sec), onTimeout);
}
}

Resources