I use json to fetch data from my database, now I want to use that dynamic data within a listview, whenever you click on any of them, it will navigate you to another page, that again uses the dynamic data that is fetched before. Here is the data model for my json:
class Base{
//the type of our object is the array
List <Device> array;
List<Device> devices;
Base( {this.devices});
factory Base.fromJson(Map<String,dynamic> parsedJson){
var list = parsedJson['devices'] as List;
List<Device> deviceList = list.map((i) => Device.fromJson(i)).toList();
return Base(
devices: deviceList
);
}
}
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>parsedJson){
//var functionFromJson=parsedJson["function"];
//List<function> functionList=functionFromJson.cast<function>();
var list = parsedJson['functions'] as List;
List<function> functionList=list.map((i)=>function.fromJson(i)).toList();
return Device(
device_desc: parsedJson["device_desc"],
device_title: parsedJson["device_title"],
image_path: parsedJson["image_path"],
status_id: parsedJson["status_id"],
functions: functionList,
statuss: Status.fromJson(parsedJson['statuss'])
);
}
}
class Status {
String status_desc, status_title;
Status({this.status_desc,this.status_title});
factory Status.fromJson(Map<String,dynamic>parsedJson){
return Status(
status_desc: parsedJson["status_desc"],
status_title: parsedJson["status_title"],
);
}
}
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> parsedJson){
return function(
function_desc: parsedJson["function_desc"],
function_title:parsedJson["function_title"],
device_id: parsedJson["device_id"],
status: parsedJson["status"]
);
}
}
Now I navigate data into second page this way: (this navigator is into a listview. builder, so, we can use the index)
onPressed: () { Navigator.push(context,MaterialPageRoute(builder: (context)=> MyHomePage2(snapshot.data.devices[index]))); },
I have defined a variable, It `s type is Device,and I use constructor to be able to specify the different dynamic data:
Class MyHomePage2 extends StatefulWidget {
final Device devices2;
MyHomePage2(this.devices2);
@override
_MyHomePage2State createState() => new _MyHomePage2State();
}
but whenever I want to use the variable named devices2, for instance, the code below:
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('خانه هوشمند'),
),
body: ListView(
//the main list containing stack , device image,device name
children: <Widget>[
new Container(
//for appropriate image size I use container
padding: EdgeInsets.all(50.0),
child: ListView(
//contains devicename and image
physics: new NeverScrollableScrollPhysics(),
shrinkWrap: true,
children: <Widget>[
ListView(
physics: new NeverScrollableScrollPhysics(),
shrinkWrap: true,
children: <Widget>[
new Image.asset('images/acc.png',fit: BoxFit.fitWidth,),
new Text('نام دستگاه',textAlign: TextAlign.right,style: TextStyle(fontSize: 25.0,color: Colors.teal),),
new Text(devices2[0].device_title,textAlign: TextAlign.right,style: TextStyle(fontSize: 20.0,color: Colors.blueGrey),),
],
),
],
),
),
It will cause an error
[dart] Undefined name 'devices2'.
Tnx for any help:)
In your Code :
new Text(devices2[0].device_title,textAlign: TextAlign.right,style: TextStyle(fontSize: 20.0,color: Colors.blueGrey),),
Change to:
new Text(widget.devices2[0].device_title,textAlign: TextAlign.right,style: TextStyle(fontSize: 20.0,color: Colors.blueGrey),),