简体   繁体   中英

How to display received json data from previous screen in Flutter

I want to display the JSON data inside ListView.builder received from the previous screen. Below is the sample code till now that 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
              }),
        ],
      ),
    );
  }
}

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);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM