Flutter:尝试将数据添加到地图列表时出错

Flutter:尝试将数据添加到地图列表时出错,第1张

概述我对Flutter非常陌生.我正在开发一个测试应用程序,而打击是我的代码.主镖import'package:flutter/material.dart';import'./pages/homepage.dart';voidmain(){runApp(MyApp());}classMyAppextendsStatelessWidget{@overrideWidgetbuild(BuildContextc

我对Flutter非常陌生.我正在开发一个测试应用程序,而打击是我的代码.

主镖

import 'package:Flutter/material.dart';import './pages/homepage.dart';voID main() {  runApp(MyApp());}class MyApp extends StatelessWidget {  @overrIDe  Widget build(BuildContext context) {    // Todo: implement build    return MaterialApp(      theme: themeData(primarycolor: colors.green),      home: Homepage(),    );  }}

homepage.dart

import 'package:Flutter/material.dart';class Homepage extends StatelessWidget {  List<Map<String, String>> productMap;  Homepage() {    productMap.add({"Title": "Chocolate", "imageUrl": ""});  }  @overrIDe  Widget build(BuildContext context) {    // Todo: implement build    return Scaffold(      appbar: Appbar(        Title: Text("Choco Factory"),      ),      body: HomepageUI(),    );  }}class HomepageUI extends StatefulWidget {  HomepageUI() {}  @overrIDe  State<StatefulWidget> createState() {    // Todo: implement createState    return _HomepageUIBuilder();  }}class _HomepageUIBuilder extends State<HomepageUI> {  @overrIDe  Widget build(BuildContext context) {    // Todo: implement build    return Expanded(      child: Column(        children: <Widget>[          ListVIEw.builder(            itemBuilder: _ListBuilder,          )        ],      ),    );  }  Widget _ListBuilder(BuildContext context, int index) {    return Card(      child: Column(        children: <Widget>[          Image.asset("name"),          Text("data"),          Raisedbutton(            onpressed: () {},            child: Text("Details"),            color: themeData().primarycolor,          )        ],      ),    );  }}

我收到以下错误,我不知道为什么.我来自Java背景,代码对我来说似乎还可以.我还有一个问题,在Java Map中我们可以向Map添加许多键值对,但是在Flutter中,似乎只有一个键值对是可能的?

Launching lib\main.dart on A37f in deBUG mode...Built build\app\outputs\apk\deBUG\app-deBUG.apk.I/Choreographer(11935): Skipped 87 frames!  The application may be doing too much work on its main thread.I/Flutter (11935): ══╡ EXCEPTION CAUGHT BY WidgetS liBRARY ╞═══════════════════════════════════════════════════════════I/Flutter (11935): The following NoSuchMethodError was thrown building MyApp(dirty):I/Flutter (11935): The method 'add' was called on null.I/Flutter (11935): Receiver: nullI/Flutter (11935): TrIEd calling: add(_linkedHashMap len:2)I/Flutter (11935): When the exception was thrown, this was the stack:I/Flutter (11935): #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)I/Flutter (11935): #1      new Homepage I/Flutter (11935): #2      MyApp.build I/Flutter (11935): #3      StatelessElement.build I/Flutter (11935): #4      ComponentElement.performRebuild I/Flutter (11935): #5      Element.rebuild I/Flutter (11935): #6      ComponentElement._firstBuild I/Flutter (11935): #7      ComponentElement.mount I/Flutter (11935): #8      Element.inflateWidget I/Flutter (11935): #9      Element.updateChild I/Flutter (11935): #10     RenderObjectToWidgetElement._rebuild I/Flutter (11935): #11     RenderObjectToWidgetElement.mount I/Flutter (11935): #12     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> I/Flutter (11935): #13     BuildOwner.buildScope I/Flutter (11935): #14     RenderObjectToWidgetAdapter.attachToRenderTree I/Flutter (11935): #15     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.attachRootWidget I/Flutter (11935): #16     runApp I/Flutter (11935): #17     main I/Flutter (11935): #18     _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:189:25)I/Flutter (11935): #23     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:180:5)I/Flutter (11935): #24     _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:300:19)I/Flutter (11935): #25     _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)I/Flutter (11935): (elIDed 4 frames from package dart:async)I/Flutter (11935): ════════════════════════════════════════════════════════════════════════════════════════════════════

解决方法:

List<Map<String, String>> productMap;

应该

List<Map<String, String>> productMap = [];

否则,仅声明productMap,而不初始化(空)

总结

以上是内存溢出为你收集整理的Flutter:尝试将数据添加到地图列表时出错全部内容,希望文章能够帮你解决Flutter:尝试将数据添加到地图列表时出错所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1092339.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存