Home > OS >  Dart - Print value from Object Array in flutter
Dart - Print value from Object Array in flutter

Time:08-03

I have an object array which says [Instance of 'OrderItems', Instance of 'OrderItems', Instance of 'OrderItems', Instance of 'OrderItems'] where 'OrderItems' is the class name. That class has three variables name,price,color. I need to parse through all of the object inside the array and should print name of all the items. I have the value as json string, i decode it using

List<OrderItems> parseOrderItems(String orderItemsString) {
  final parsed = jsonDecode(orderItemsString).cast<Map<String, dynamic>>();
  return parsed.map<OrderItems>((json) => OrderItems.fromJson(json)).toList();
}

Future<List<OrderItems>> fetchOrderItems( String orderItemsString) async {
    String orderItems = orderItemsString;
    return compute(parseOrderItems, orderItems);
  }

Now i access it using futurebuilder like below::

FutureBuilder(
                                        future: fetchOrderItems(orderData['order']),
                                      builder: (context, snapshot) {
                                        if (snapshot.hasError) {
                                          return Center(
                                            child:Text("${snapshot.error}"),
                                          );
                                        } else if (snapshot.hasData) {
                                          return Text(snapshot.data!.toString);
                                        } else {
                                          return const Center(
                                            child: CircularProgressIndicator(),
                                          );
                                        }
                                      },)

The snapshot.data!.tostring() prints ['Instance of OrderItems', Instance of 'OrderItems']. Now instead of snapshot.data!.toString() i need to print every object's name value.

CodePudding user response:

Create the model class of OrderItems using online json to dart (Google it ) then map your values into the Model class, if you want to access/Print the single value present inside the model object then use the index value of object.

this.OrderItems.name or this.OrderItems[0].name

CodePudding user response:

Your snapshot.data is a list of instances of your OrderItems. I recommend using a ListView.builder or ListView.seperated to loop through each item in the list and render the data.

https://www.geeksforgeeks.org/listview-builder-in-flutter/

https://api.flutter.dev/flutter/widgets/ListView/ListView.separated.html

Try this:

else if (snapshot.hasData) {
        return ListView.separated(
                separatorBuilder: (BuildContext context, int index) {
                  return SizedBox(
                    height: 30.0,
                  );
                },
              padding: EdgeInsets.all(50.0),
              itemCount: snapShot.data.length,
              itemBuilder: (context, index){
               // or return Text(snapshot.data[index].to_s); 
                return Text(snapshot.data[index].name);
              });
    }
  • Related