I am trying to pass data from one screen to another screen.
List<SubCategoryData>categoryNames = new List<SubCategoryData>();
List<String>categorieslist = [];
bool isFirst=true;
Future<SubCategoryModel>fetchCategories(BuildContext context) async {
String url = "http://106.51.64.251:380/onnet_api/subcatListByCategory.php";
var body = new Map<String,String>();
body['publisherid']= 102.toString();
body['tag'] = "category";
body['subtag']= "list";
body['parentId'] = 10.toString();
http.Response res = await http.post(url,body: body);
final categoryjsondata = json.decode(res.body);
var map = Map<String,dynamic>.from(categoryjsondata);
var categoryResponse = SubCategoryModel.fromJson(map);
if(res.statusCode == 200){
print('category Response: $categoryResponse');
if(categoryResponse.status == 1){
//final categoryModel = json.decode(res.body);
var data = categoryjsondata['data']as List;
print('category data: $data');
/* for(var model in categorieslist){
categoryNames.add(new SubCategoryData.fromJson(model));
}*/
/* SharedPreferences prefs = await SharedPreferences.getInstance();
print("cat List Size: $categories");
prefs.setStringList("categorylist", categories);*/
Navigator.push(context, MaterialPageRoute(builder: (context)=> ChewieDemo(imageData: images[0],
categoryData:data)));
}
}
}
By using the above code I am trying to send data but I am facing issue like type 'List' is not a subtype of type 'SubCategoryData' in type cast"
got an error and even I am not getting how to send data with an index value.Please let me know.
Below is my ChewieDemo class: Here I am trying to receive the data from another class.
class ChewieDemo extends StatefulWidget {
final Datum imageData;
final SubCategoryData categoryData;
ChewieDemo({this.title = 'Player',Key key,@required this.imageData,@required this.categoryData}): super(key:key);
final String title;
@override
State<StatefulWidget> createState() {
return _ChewieDemoState();
}
}
class _ChewieDemoState extends State<ChewieDemo> {
TargetPlatform _platform;
VideoPlayerController _videoPlayerController1;
VideoPlayerController _videoPlayerController2;
ChewieController _chewieController;
@override
void initState() {
super.initState();
print('url player :${widget.imageData.dataUrl}');
print(widget.categoryData);
// 'https://www.sample-videos.com/video123/mp4/480/big_buck_bunny_480p_20mb.mp4'
_videoPlayerController1 = VideoPlayerController.network('${widget.imageData.dataUrl}');
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController1,
aspectRatio: 3 / 2,
autoPlay: true,
looping: true,
// Try playing around with some of these other options:
// showControls: false,
// materialProgressColors: ChewieProgressColors(
// playedColor: Colors.red,
// handleColor: Colors.blue,
// backgroundColor: Colors.grey,
// bufferedColor: Colors.lightGreen,
// ),
// placeholder: Container(
// color: Colors.grey,
// ),
// autoInitialize: true,
);
}
This is the model class of SubCategoryData.
class SubCategoryData {
int id;
int parentId;
String name;
int contentCount;
String createdAt;
int status;
SubCategoryData({
this.id,
this.parentId,
this.name,
this.contentCount,
this.createdAt,
this.status,
});
factory SubCategoryData.fromJson(Map<String, dynamic> json) => new SubCategoryData(
id: json["id"],
parentId: json["parent_id"],
name: json["name"],
contentCount: json["content_count"],
createdAt: json["createdAt"],
status: json["status"],
);
Map<String, dynamic> toJson() => {
"id": id,
"parent_id": parentId,
"name": name,
"content_count": contentCount,
"createdAt": createdAt,
"status": status,
};
@override
String toString() {
// TODO: implement toString
return '$id $parentId $name $contentCount';
}
}
1. Add the dependency
Before starting, you need to add the shared_preferences plugin to the pubspec.yaml file:
content_copy
dependencies:
flutter:
sdk: flutter
shared_preferences: "<newest version>"
2. Save data
To persist data, use the setter methods provided by the SharedPreferences class. Setter methods are available for various primitive types, such as setInt, setBool, and setString.
Setter methods do two things: First, synchronously update the key-value pair in-memory. Then, persist the data to disk.
// obtain shared preferences
final prefs = await SharedPreferences.getInstance();
// set value
prefs.setInt('counter', counter);
3. Read data
To read data, use the appropriate getter method provided by the SharedPreferences class. For each setter there is a corresponding getter. For example, you can use the getInt, getBool, and getString methods.
final prefs = await SharedPreferences.getInstance();
// Try reading data from the counter key. If it does not exist, return 0.
final counter = prefs.getInt('counter') ?? 0;
4. Remove data
To delete data, use the remove method.
content_copy
final prefs = await SharedPreferences.getInstance();
prefs.remove('counter');
you are getting a list of SubCategoryData
from the httpcall
. If you need to pass a List
of your SubCategoryData
model you need first to fix the following in your ChewieDemo
class
class ChewieDemo extends StatefulWidget {
final Datum imageData;
final List<SubCategoryData> categoryData;
ChewieDemo({this.title = 'Player',Key key,@required this.imageData,@required this.categoryData}): super(key:key);
final String title;
@override
State<StatefulWidget> createState() {
return _ChewieDemoState();
}
}
and when you push the following:
var categoryData = categoryjsondata['data'] as List;
print('category data: $categoryData');
for(var model in categoryData){
categoryNames.add(new SubCategoryData.fromJson(model));
}
print("cat List Size: $categoryData");
Navigator.push(context, MaterialPageRoute(builder: (context)=> ChewieDemo(imageData: null, categoryData: categoryNames));
where categoryNames is a List<SubCategoryData>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.