Json没有响应

bq3bfh9z  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(209)

我正在尝试在我的widget中初始化json数据,但是由于某种原因,它没有响应。我对处理本地调用的json很陌生,这是其中之一。我已经做了所有必要的事情,比如创建文件来放置数据并通过pubspec传递它。它仍然没有响应。下面是json数据

[
   {
      "_id":"636e0ce55270d648e9a5248a",
      "index":0,
      "guid":"58e73438-27fa-48bf-8127-ff4e42beaac5",
      "isActive":true,
      "price":"$252.77",
      "name":"James Lewis Blue Ledis",
      "type":"dress",
      "image":"https://images.unsplash.com/photo-1539008835657-9e8e9680c956?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&q=80"
   },
   {
      "_id":"636e0ce573d4e1ce44e5a5a9",
      "index":1,
      "guid":"e1d93cfe-b15a-4969-97e0-3b6b9ae967d0",
      "isActive":false,
      "price":"$289.88",
      "name":"Blue FLower Shoe",
      "type":"apparel",
      "image":"https://images.unsplash.com/photo-1543163521-1bf539c55dd2?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=880&q=80"
   },
   {
      "_id":"636e0ce522bd392da7b43d2e",
      "index":2,
      "guid":"0ee56032-db50-44c3-9234-5e213b8fde22",
      "isActive":false,
      "price":"$452.81",
      "name":"Yellow Blight Bag",
      "type":"bag",
      "image":"https://images.pexels.com/photos/934673/pexels-photo-934673.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1",
      "recommended":true
   },
   {
      "_id":"636e0ce5fdf98a105a5c14cb",
      "index":3,
      "guid":"fee6217c-ee7f-4e3e-91cc-24dfd91d6cfc",
      "isActive":true,
      "price":"$244.91",
      "name":"Black meter dress",
      "type":"dress",
      "image":"https://images.unsplash.com/photo-1550639525-c97d455acf70?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=726&q=80"
   },
   {
      "_id":"636e0ce5036de010a96ab185",
      "index":4,
      "guid":"2d9e8b22-90c1-4979-b904-2cc164626e10",
      "isActive":false,
      "price":"$335.17",
      "name":"Dynamic Pink Lobre",
      "type":"bag",
      "image":"https://images.unsplash.com/photo-1566150905458-1bf1fc113f0d?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1171&q=80"
   },
   {
      "_id":"636e0ce52e2a60eed71c79b6",
      "index":5,
      "guid":"32db02a5-a12d-4f3e-a601-787211734ab1",
      "isActive":false,
      "price":"$414.69",
      "name":"Gucci Flems Onyx",
      "type":"bag",
      "image":"https://images.unsplash.com/photo-1548036328-c9fa89d128fa?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1169&q=80"
   },
   {
      "_id":"636e0ce52f14c39bb8366650",
      "index":6,
      "recommended":true,
      "guid":"1a0c388c-2ac7-4a49-85ba-50b12bc08f28",
      "isActive":true,
      "price":"$293.63",
      "name":"705 California",
      "type":"apparel",
      "image":"https://images.unsplash.com/photo-1618354691229-88d47f285158?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=715&q=80"
   },
   {
      "_id":"636e0ce56b57e56f54676302",
      "index":7,
      "guid":"956527b1-c9d8-4c18-a6ef-e4b1dcd76640",
      "isActive":false,
      "price":"$293.57",
      "name":"Mid-week flavors",
      "type":"apparel",
      "image":"https://images.unsplash.com/photo-1560769629-975ec94e6a86?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=764&q=80"
   },
   {
      "_id":"636e0ce56c0ed15b28297895",
      "index":8,
      "guid":"119db62a-eaba-493f-b73a-5dcba04b69c1",
      "isActive":false,
      "price":"$348.63",
      "name":"Hermes Coysx",
      "type":"bag",
      "image":"https://images.unsplash.com/photo-1594223274512-ad4803739b7c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=757&q=80"
   },
   {
      "_id":"636e0ce58d2cbb1e636f1829",
      "index":9,
      "guid":"40011d30-d06d-4a72-b119-0826dc79c138",
      "isActive":true,
      "price":"$337.91",
      "name":"Kirsten Gilliam",
      "type":"dress",
      "image":"https://images.unsplash.com/photo-1543163521-1bf539c55dd2?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=880&q=80"
   },
   {
      "_id":"636e0ce5713679af9a884699",
      "index":10,
      "guid":"f5dbfba4-fbe2-475b-98fc-36ff42daad91",
      "isActive":false,
      "price":"$436.62",
      "recommended":true,
      "name":"Outcast white",
      "type":"t-shirt",
      "image":"https://images.unsplash.com/photo-1527719327859-c6ce80353573?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=764&q=80"
   },
   {
      "_id":"636e0ce5578fa1d92624204f",
      "index":11,
      "guid":"805df707-3833-4e5f-9e09-0c59053750cb",
      "isActive":false,
      "price":"$493.15",
      "name":"Peace Skull",
      "type":"t-shirt",
      "image":"https://images.unsplash.com/photo-1503341504253-dff4815485f1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&q=80"
   },
   {
      "_id":"636e0ce5bd29b6bd570aef00",
      "index":12,
      "guid":"32e75d5c-ea59-4c23-8d85-29dd37040a7d",
      "isActive":false,
      "price":"$350.71",
      "name":"Louis Vuiton Brown bag",
      "type":"bag",
      "image":"https://images.pexels.com/photos/3661622/pexels-photo-3661622.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"
   },
   {
      "_id":"636e0ce505794be5900eb9ad",
      "index":13,
      "guid":"44aa2a9a-e1c9-4004-b681-ce246e5a8ee4",
      "isActive":true,
      "price":"$271.21",
      "name":"Impulso Supreme",
      "type":"t-shirt",
      "recommended":true,
      "image":"https://images.unsplash.com/photo-1627933540891-1fb6a397c89b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=764&q=80"
   }
]

这就是它被称为

class ExploreCollections extends StatefulWidget {
  const ExploreCollections({Key? key}) : super(key: key);

  @override
  State<ExploreCollections> createState() => _ExploreCollectionsState();
}

class _ExploreCollectionsState extends State<ExploreCollections> {
  final List _items = [];

  Future<void> readJson() async{
    final String response = await rootBundle.loadString('assets/model/clothes.json');
    final data = await json.decode(response);
  }
  @override
  void initState() {
    _items.shuffle();
    super.initState();
  }

  int _activeIndex = 0;
  final List<Widget> _images = [
    Stack(
      children: [
        Image.asset('assets/images/image 10.png'),
        Padding(
          padding: const EdgeInsets.only(left: 55.0, top: 230),
          child: Text(
            'Luxury \n   Fashion \n &Accessories'.toUpperCase(),
            style: TextStyle(
                fontFamily: 'Bodoni',
                fontSize: 40,
                fontWeight: FontWeight.w500,
                color: Colors.grey.shade700
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(top: 400.0),
          child: Center(
            child:SvgPicture.asset('assets/iconImages/Button.svg'),
          ),
        ),
      ],
    ),
    Stack(
      children: [
        Image.asset('assets/images/leeloo.jpeg'),
      ],
    ),
    Stack(
      children: [
        Image.asset('assets/images/ayaka.jpeg'),
      ],
    ),
  ];
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 5,
      child: Column(
        children: [
          Stack(
            children: [
              CarouselSlider.builder(
                options: CarouselOptions(
                  viewportFraction: 1,
                  aspectRatio: 1.8,
                  height: MediaQuery.of(context).size.height*0.8,
                  autoPlay: false,
                  initialPage: 0,
                  enableInfiniteScroll: false,
                  enlargeCenterPage: true,
                  onPageChanged: (index, reason){
                    setState(() {
                      _activeIndex = index;
                    });
                  }
                ),
                itemCount: _images.length,
                itemBuilder: (BuildContext context, int index, int realIndex) {
                  return GestureDetector(
                    onTap: (){
                      Navigator.of(context).pushNamedAndRemoveUntil(BlackScreen.routeName, (route) => false);
                    },
                      child: _images[index]);
                },
              ),
              Padding(
                padding: const EdgeInsets.only(top: 565.0),
                child: Center(
                  child: buildIndicator(),
                ),
              ),
            ],
          ),
          SvgPicture.asset('assets/images/Title.svg'),
          const SizedBox(
            height: 10,),
          TabBar(
            indicator: CircleTabIndicator(color: Colors.redAccent, radius: 3),
              tabs: [
                Tab(child: Text('All',style: TextStyle(color: Colors.grey.shade600),),),
                Tab(child: Text('Apparel',style: TextStyle(color: Colors.grey.shade600),),),
                Tab(child: Text('Dress',style: TextStyle(color: Colors.grey.shade600),),),
                Tab(child: Text('Tshirt',style: TextStyle(color: Colors.grey.shade600),),),
                Tab(child: Text('Bag',style: TextStyle(color: Colors.grey.shade600),),),
              ]
          ),

          Container(
            height: 60,
            child: TabBarView(
                children: [
                  All(items: _items),
                  Center(child: Text('Apparel'),),
                  Center(child: Text('Dress'),),
                  Center(child: Text('Tshirt'),),
                  Center(child: Text('Bag'),),
                ]
            ),
          )
        ],
      ),
    );
  }

我终于把它传给了“所有班级”

class All extends StatelessWidget {
  final List items;
  const All({Key? key, required this.items}) : super(key: key);

  

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: items.length,
        itemBuilder: (context, index){
         return Text(items[index]['name']);
        });
  }
}
j13ufse2

j13ufse21#

第一步,应将_list变量的类型更改为Map<String,dynamic>
并更改readJson方法的返回类型

Future<Map<String,dynamic>> readJson() async{
    final String response = await rootBundle.loadString('assets/model/clothes.json');
    return await json.decode(response);
  }

initState中调用readJson,如下所示

@override
  void initState() {
     WidgetsBinding.instance.addPostFrameCallback((_) async {
         readJson().then((value) =>_items = value);
     });
    super.initState();
  }

这个应该可以

相关问题