NoSuchMethodError: 在 null 上调用了方法“[]”。颤振本地json文件

时间:2021-07-14 12:49:02

标签: flutter

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常:NoSuchMethodError:方法 '[]' 在 null 上被调用。

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter ( 8471): Receiver: null
E/flutter ( 8471): Tried calling: []("runnerId")
E/flutter ( 8471): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
E/flutter ( 8471): #1      new Metadata.fromMap (package:jsonfatch/model/event.dart:348:22)
E/flutter ( 8471): #2      new MarketRunner.fromMap (package:jsonfatch/model/event.dart:329:28)
E/flutter ( 8471): #3      new Market.fromMap.<anonymous closure> (package:jsonfatch/model/event.dart:138:52)
E/flutter ( 8471): #4      MappedListIterable.elementAt (dart:_internal/iterable.dart:417:31)
E/flutter ( 8471): #5      ListIterator.moveNext (dart:_internal/iterable.dart:343:26)
E/flutter ( 8471): #6      new List.from (dart:core-patch/array_patch.dart:57:19)
E/flutter ( 8471): #7      new Market.fromMap (package:jsonfatch/model/event.dart:137:18)
E/flutter ( 8471): #8      new EventDetails.fromMap.<anonymous closure> (package:jsonfatch/model/event.dart:65:62)
E/flutter ( 8471): #9      MappedListIterable.elementAt (dart:_internal/iterable.dart:417:31)
E/flutter ( 8471): #10     ListIterator.moveNext (dart:_internal/iterable.dart:343:26)
E/flutter ( 8471): #11     new List.from (dart:core-patch/array_patch.dart:57:19)
E/flutter ( 8471): #12     new EventDetails.fromMap (package:jsonfatch/model/event.dart:65:11)
E/flutter ( 8471): #13     HomePageState.loadData.<anonymous closure> (package:jsonfatch/home.dart:28:51)
E/flutter ( 8471): #14     MappedListIterable.elementAt (dart:_internal/iterable.dart:417:31)
E/flutter ( 8471): #15     ListIterator.moveNext (dart:_internal/iterable.dart:343:26)
E/flutter ( 8471): #16     new List.from (dart:core-patch/array_patch.dart:38:29)
E/flutter ( 8471): #17     new List.of (dart:core-patch/array_patch.dart:68:17)
E/flutter ( 8471): #18     ListIterable.toList (dart:_internal/iterable.dart:211:44)
E/flutter ( 8471): #19     HomePageState.loadData (package:jsonfatch/home.dart:29:10)
E/flutter ( 8471): <asynchronous suspension>
E/flutter ( 8471): #20     HomePageState.initState (package:jsonfatch/home.dart:19:5)
E/flutter ( 8471): #21     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4765:58)   
E/flutter ( 8471): #22     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
E/flutter ( 8471): #23     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
E/flutter ( 8471): #24     Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
E/flutter ( 8471): #25     RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1252:16)
E/flutter ( 8471): #26     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1223:5)E/flutter ( 8471): #27     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1165:17)
E/flutter ( 8471): #28     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2683:19)
E/flutter ( 8471): #29     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1164:13)
E/flutter ( 8471): #30     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:974:7)   
E/flutter ( 8471): #31     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:955:7)
E/flutter ( 8471): #32     _rootRun (dart:async/zone.dart:1182:47)
E/flutter ( 8471): #33     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 8471): #34     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 8471): #35     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23) 
E/flutter ( 8471): #36     _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 8471): #37     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 8471): #38     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter ( 8471): #39     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)   
E/flutter ( 8471): #40     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter ( 8471): #41     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter ( 8471): #42     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12) 

我的 json 文件,其中的数据是嵌套的 key value per and do many json obj

"runners":[
           {
              "id":10301,
              "name":"roman",
              "sortPriority":0,
              "metadata":{
                 "runnerId":"10301"
              }
           },
           {
              "id":7337,
              "name":"khali",
              "sortPriority":0,
              "metadata":{
                 "runnerId":"7337"
              }
           }
        ],

我的 runnerId 地图代码。它是一个嵌套的数组对象。其他所有我在模型文件中映射的

class Metadata {
  Metadata({
    this.runnerId,
  });

  String runnerId;

  factory Metadata.fromMap(Map<String, dynamic> map) => Metadata(
        runnerId: map["runnerId"],
      );

  Map<String, dynamic> toMap() => {
        "runnerId": runnerId,
      };
}

我的家.dart 代码

class HomePage extends StatefulWidget {
  @override
  HomePageState createState() => HomePageState();
}

class HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    loadData();
  }

  loadData() async {
    await Future.delayed(Duration(seconds: 2));
    var cktJson = await rootBundle.loadString("assets/files/ckt.json");
    var decodedData = jsonDecode(cktJson);
    var cricketData = decodedData["cktdata"];
    EventsModel.markets = List.from(cricketData)
        .map<EventDetails>((item) => EventDetails.fromMap(item))
        .toList();
    setState(() {});
    print("Decoded :-"+decodedData);
    print("CrircktD :-"+ cricketData);
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: MainBackgroundColor,
          title: Text("Radhe Json Fatch"),
        ),
        body: SafeArea(
          child: Column(
            children: [
              Expanded(
                child: (EventsModel.markets != null && EventsModel.markets.isNotEmpty)?
                ListView.builder(
                  scrollDirection: Axis.vertical,
                  itemCount: EventsModel.markets.length,
                  itemBuilder: (context, index) {
                    return LiveEventCricket(item: EventsModel.markets[index]);
                  },
                ):Center(child: CircularProgressIndicator()),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

0 个答案:

没有答案