Home > Mobile >  How to call foreach if array is locked in string?
How to call foreach if array is locked in string?

Time:10-23

When I am saving my data to phpMyAdmin with API it converts to a string and then its saved when I fetch it comes in string like "[{id: 15}, {id: 150}]" but I want this as [{id: 15}, {id: 150}]"[{id: 15}, {id: 150}] in flutter

class _SaleShowState extends State<SaleShow> {
  bool saleDetailsFetched = false;
  var saleDetails;

  @override
  void initState() {
    fetchSaleDetails();
    super.initState();
  }

  fetchSaleDetails() async {
    var response = await SaleRepository().showSale(widget.saleId);
    if (response['result'] == true) {
      setState(() {
        saleDetails = response['sales'];
        saleDetailsFetched = true;
      });
    } else {
      showToast("Something went wrong", context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return ScreenTemplate(
      screen: (saleDetailsFetched == true)
          ? Container(
              height: MediaQuery.of(context).size.height,
              width: MediaQuery.of(context).size.width,
              padding: const EdgeInsets.all(16),
              color: MyTheme.background_color,
              child: Column(
                children: [
                  Text(saleDetails['items'].replaceAll('"', "").toList())
                  // Container(
                  //   height: 250,
                  //   child: ListView.builder(
                  //     itemCount: saleDetails['items'].length,
                  //     itemBuilder: (context, index) {
                  //       return Container(
                  //         child: Text("hey"),
                  //       );
                  //     },
                  //   ),
                  // )
                ],
              ),
            )
          : Container(),
    );
  }
}

What I do

CodePudding user response:

You need to decode it like this:

var str = "[{id: 15}, {id: 150}]";
List _list = jsonDecode(str);

now you have your list and can call foreach on it.

  • Related