Home > OS >  How to display received json data from previous screen in Flutter
How to display received json data from previous screen in Flutter

Time:03-17

I want to display the json data inside ListView.builder receiving from previous screen. Below is the sample code till now what i have tried.

FirstPage.dart

Navigator.push(
              context,
              CupertinoPageRoute(
                  builder: (context) => MyOrderDetails(
                        storeItems: order.inDetail!.menuItems!
                      )));

This is the sample json i am passing to Next Screen

{
                        "item_name": "Test",
                        "quantity": 1,
                        "subtotal": "434.78"
                    }
                

MyOrderDetail.dart

class MyOrderDetails extends StatefulWidget {
  final List storeItems;
  const MyOrderDetails(
      {Key? key,
      required this.storeItems})
      : super(key: key);

  @override
  State<MyOrderDetails> createState() => _MyOrderDetailsState();
}

class _MyOrderDetailsState extends State<MyOrderDetails> {
  @override
  Widget build(BuildContext context) {
    var height = MediaQuery.of(context).size.height;
    var width = MediaQuery.of(context).size.width;
    var lang = translator.activeLanguageCode;
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
      ),
      body: ListView(
        children: [
          ListView.builder(
              shrinkWrap: true,
              scrollDirection: Axis.vertical,
              itemCount: widget.storeItems.length,
              itemBuilder: (BuildContext context, int index) {
                return Text(widget.storeItems[index]['item_name']); // Getting error here
              }),
        ],
      ),
    );
  }
}

CodePudding user response:

Your variable should be like that. List is not enough by itself you should declare which class is for that list.

final List<YourDataClass> storeItems;
  const MyOrderDetails(
      {Key? key,
      required this.storeItems})
      : super(key: key);
  • Related