开发者

flutter : RangeError (index): Invalid value: Only valid value is 0: 1 api

I can't fetch the next address

but I can fetch sequence shipto 0

RangeError (index): Invalid value: Only valid value is 0: 1

I search by name and it shows the address part of that name. But what happens is it shows only the first address. The next rank is not showing.

List<Item> listitem = [];
var loadin开发者_运维百科g = false;
Future<List<Item>> fetchMos() async {
 listitem.clear();
 var client = http.Client();
  String mosUrl =
     '';
 var url = Uri.parse(mosUrl);
 var headers = {'Client-Token': ''};
 var response = await client.get(url, headers: headers);
if (response.statusCode == 200) {
var data = jsonDecode((utf8.decode(response.bodyBytes)))['items'];
for (var index in data) {
  listitem.add(Item.fromJson(index));
  print(data);
}
loading = false;
  }
 return listitem;
 }


@override
  void initState() {
fetchMos().then((value) {
  setState(() {
    items.addAll(value);
  });
  super.initState();
});

This is my search code.

 onSearch(String text) async {
searchitems.clear();
if (text.isEmpty) {
  setState(() {
    loading = false;
  });
  return;
}

items.forEach((f) {
  if (f.custnum.toString().toUpperCase().toLowerCase().contains(text))
    searchitems.add(f);
});

setState(() {});

It is the part that displays the address information

    child: searchitems.length != null ||
                      controller.text.isNotEmpty
                  ? ListView.builder(
                      controller: controllerlistview,
                      itemCount: searchitems.length,
                      itemBuilder: (context, index) {
                        return Card(
                          elevation: 3,
                          child: Column(
                            children: <Widget>[
                              Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: <Widget>[
                                  TextButton(
                                    child: const Text(
                                      'แก้ไข',
                                      style: TextStyle(
                                          fontFamily: "supermarket",
                                          fontSize: 14),
                                    ),
                                    onPressed: () {
                                      Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) =>
                                                  EditAddressPage()));
                                    },
                                  ),
                                ],
                              ),
                              Padding(
                                padding: const EdgeInsets.all(5),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.start,
                                  children: [
                                    Text(
                                      'bill To / Ship To : ${searchitems[index].address![index].shipto.toString()}',
                                      style: TextStyle(
                                          color: Colors.black,
                                          fontSize: 16,
                                          fontFamily: "supermarket"),
                                    ),
                                  ],
                                ),
                              ),
                              Padding(
                                padding: const EdgeInsets.all(5),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.start,
                                  children: [
                                    Text(
                                      "ที่อยู่ : ${searchitems[index].address![index].addr1.toString()}",
                                      style: TextStyle(
                                          color: Colors.black,
                                          fontSize: 16,
                                          fontFamily: "supermarket"),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        );
                      })

This is json

{
  "items": [
{
    "custnum": "",
    "name": "",
    "address": [
        {
            "shipto": 0,
            "addr1": "",
            "thanon": "",
            "tambon": "",
            "amphur": "",
            "prov_code": "",
            "province": "",
            "zipcode": "",
            "country": "",
            "contact": "",
            "postcode": ""
        },
        {
           "shipto": 1,
            "addr1": "",
            "thanon": "",
            "tambon": "",
            "amphur": "",
            "prov_code": "",
            "province": "",
            "zipcode": "",
            "country": "",
            "contact": "",
            "postcode": ""
        },
        {
            "shipto": 2,
            "addr1": "",
            "thanon": "",
            "tambon": "",
            "amphur": "",
            "prov_code": "",
            "province": "",
            "zipcode": "",
            "country": "",
            "contact": "",
            "postcode": ""
        },
        {
            "shipto": 3,
            "addr1": "",
            "thanon": "",
            "tambon": "",
            "amphur": "",
            "prov_code": "",
            "province": "",
            "zipcode": "",
            "country": "",
            "contact": "",
            "postcode": ""
        }
    ]

},


{
"custnum": "",
"name": "",
"address": [
    {
        "shipto": 0,
        "addr1": "",
        "thanon": "",
        "tambon": "",
        "amphur": "",
        "prov_code": "",
        "province": "",
        "zipcode": "",
        "country": "",
        "contact": "",
        "postcode": ""
    },
    {
       "shipto": 1,
        "addr1": "",
        "thanon": "",
        "tambon": "",
        "amphur": "",
        "prov_code": "",
        "province": "",
        "zipcode": "",
        "country": "",
        "contact": "",
        "postcode": ""
    },
    {
        "shipto": 2,
        "addr1": "",
        "thanon": "",
        "tambon": "",
        "amphur": "",
        "prov_code": "",
        "province": "",
        "zipcode": "",
        "country": "",
        "contact": "",
        "postcode": ""
    },
    {
        "shipto": 3,
        "addr1": "",
        "thanon": "",
        "tambon": "",
        "amphur": "",
        "prov_code": "",
        "province": "",
        "zipcode": "",
        "country": "",
        "contact": "",
        "postcode": ""
    }

],

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜