错误:flutter/lib/ui/ui_dart_state.cc(177) 未处理的异常:NoSuchMethodError:方法“插入”在 null 上调用
Posted
技术标签:
【中文标题】错误:flutter/lib/ui/ui_dart_state.cc(177) 未处理的异常:NoSuchMethodError:方法“插入”在 null 上调用【英文标题】:ERROR:flutter/lib/ui/ui_dart_state.cc(177) Unhandled Exception: NoSuchMethodError: The method 'insert' was called on null 【发布时间】:2021-04-04 15:27:51 【问题描述】:你好,我是一个 Flutter 开发者,我想用我的云 Firestore 数据库中的数据制作一个列表。我不知道我该怎么做。任何有关我的代码的帮助或建议将不胜感激
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:dinghy1/components/default_button.dart';
import 'package:dinghy1/services/reservations.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import '../../../constants.dart';
import '../../../size_config.dart';
class Body extends StatefulWidget
@override
_BodyState createState() => _BodyState();
class _BodyState extends State<Body>
static List<Map<String, dynamic>> reservations;
static Map<String, dynamic> documents;
static List<QueryDocumentSnapshot> documents2;
static int index;
bool visibility = false;
String rental = ReservationService.rental;
@override
Widget build(BuildContext context)
return SingleChildScrollView(
child: SafeArea(
child: Column(
children: [
SizedBox(
height: getPropotionateScreenWidth(20),
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: getPropotionateScreenWidth(20)),
child: Container(
decoration: BoxDecoration(
color: kPrimaryColor.withOpacity(0.4),
borderRadius: BorderRadius.circular(30)),
height: getPropotionateScreenHeight(75),
width: double.infinity,
child: Center(
child: Text(
"Sprawdź rezerwacje",
style: TextStyle(
fontSize: getPropotionateScreenWidth(20),
fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
),
),
SizedBox(
height: getPropotionateScreenWidth(20),
),
DatePicker(),
SizedBox(
height: getPropotionateScreenWidth(20),
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: getPropotionateScreenWidth(20)),
child: DefaultButton(
text: "Sprawdź",
press: ()
final FirebaseAuth auth = FirebaseAuth.instance;
final User user = auth.currentUser;
String uid = user.uid;
FirebaseFirestore.instance
.collection("users")
.doc(uid)
.get()
.then((DocumentSnapshot documentSnapshot)
if (documentSnapshot.exists)
rental = documentSnapshot['rental'].toString();
ReservationService()
.getReservations(
rental,
_DatePickerState.selectedDate
.toString()
.split(" ")[0])
.then((QuerySnapshot docs)
if (docs.docs.isNotEmpty)
int i = 0;
index = docs.size - 1;
print(index);
while (i <= index)
documents = docs.docs[i].data();
if (documents != null)
reservations.insert(i, documents);
i++;
print(documents);
);
);
setState(()
RentCard.name = documents['name'];
visibility = true;
);
,
),
),
SizedBox(
height: getPropotionateScreenWidth(20),
),
visibility == true
? new Padding(
padding: EdgeInsets.symmetric(
horizontal: getPropotionateScreenWidth(20)),
child: Column(
children: [
// ...List.generate(
// documents2.length,
// (index) => RentCard(reservations: reservations[0]),
// ),
],
),
)
: new Text(
'Wybierz date i naciśnij przycisk aby sprawdzić rezerwacje!')
],
),
),
);
这是出错的代码示例
E/flutter ( 5082): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method 'insert' was called on null.
E/flutter ( 5082): Receiver: null
E/flutter ( 5082): Tried calling: insert(0, _LinkedHashMap len:7)
E/flutter ( 5082): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
E/flutter ( 5082): #1 _BodyState.build.<anonymous closure>.<anonymous closure>.<anonymous closure>
package:dinghy1/…/components/body.dart:89
E/flutter ( 5082): #2 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 5082): #3 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 5082): #4 _FutureListener.handleValue (dart:async/future_impl.dart:143:18)
E/flutter ( 5082): #5 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45)
E/flutter ( 5082): #6 Future._propagateToListeners (dart:async/future_impl.dart:725:32)
E/flutter ( 5082): #7 Future._completeWithValue (dart:async/future_impl.dart:529:5)
E/flutter ( 5082): #8 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:40:15)
E/flutter ( 5082): #9 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:311:13)
E/flutter ( 5082): #10 Query.get (package:cloud_firestore/src/query.dart)
package:cloud_firestore/src/query.dart:1
E/flutter ( 5082): <asynchronous suspension>
E/flutter ( 5082): #11 ReservationService.getReservations
package:dinghy1/services/reservations.dart:19
E/flutter ( 5082): #12 _BodyState.build.<anonymous closure>.<anonymous closure>
package:dinghy1/…/components/body.dart:76
E/flutter ( 5082): #13 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 5082): #14 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 5082): #15 _FutureListener.handleValue (dart:async/future_impl.dart:143:18)
E/flutter ( 5082): #16 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45)
E/flutter ( 5082): #17 Future._propagateToListeners (dart:async/future_impl.dart:725:32)
E/flutter ( 5082): #18 Future._completeWithValue (dart:async/future_impl.dart:529:5)
E/flutter ( 5082): #19 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:40:15)
E/flutter ( 5082): #20 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:311:13)
E/flutter ( 5082): #21 DocumentReference.get (package:cloud_firestore/src/document_reference.dart)
package:cloud_firestore/src/document_reference.dart:1
E/flutter ( 5082): <asynchronous suspension>
E/flutter ( 5082): #22 _BodyState.build.<anonymous closure>
package:dinghy1/…/components/body.dart:71
E/flutter ( 5082): #23 _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:993
E/flutter ( 5082): #24 _InkResponseState.build.<anonymous closure>
package:flutter/…/material/ink_well.dart:1111
E/flutter ( 5082): #25 GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:183
E/flutter ( 5082): #26 TapGestureRecognizer.handleTapUp
package:flutter/…/gestures/tap.dart:598
E/flutter ( 5082): #27 BaseTapGestureRecognizer._checkUp
package:flutter/…/gestures/tap.dart:287
E/flutter ( 5082): #28 BaseTapGestureRecognizer.handlePrimaryPointer
package:flutter/…/gestures/tap.dart:222
E/flutter ( 5082): #29 PrimaryPointerGestureRecognizer.handleEvent
package:flutter/…/gestures/recognizer.dart:476
E/flutter ( 5082): #34 PointerRouter.route
package:flutter/…/gestures/pointer_router.dart:106
E/flutter ( 5082): #35 GestureBinding.handleEvent
package:flutter/…/gestures/binding.dart:358
E/flutter ( 5082): #36 GestureBinding.dispatchEvent
package:flutter/…/gestures/binding.dart:338
E/flutter ( 5082): #37 RendererBinding.dispatchEvent
package:flutter/…/rendering/binding.dart:267
E/flutter ( 5082): #38 GestureBinding._handlePointerEvent
package:flutter/…/gestures/binding.dart:295
E/flutter ( 5082): #39 GestureBinding._flushPointerEventQueue
package:flutter/…/gestures/binding.dart:240
E/flutter ( 5082): #40 GestureBinding._handlePointerDataPacket
package:flutter/…/gestures/binding.dart:213
E/flutter ( 5082): #41 _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter ( 5082): #42 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 5082): #43 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 5082): #44 _invoke1 (dart:ui/hooks.dart:265:10)
E/flutter ( 5082): #45 _dispatchPointerDataPacket (dart:ui/hooks.dart:174:5)
E/flutter ( 5082):
这是一条错误消息。 如果有人能帮我解决这个问题,我会幸福得发疯的
【问题讨论】:
以这种方式定义列表:static List<Map<String, dynamic>> reservations = [];
非常感谢@Arytan 的帮助!!
static Map<String, dynamic> documents = ;
static List<QueryDocumentSnapshot> documents2 = [];
当我试图解决这个问题时,我删除了获取 docs.docs 并将其发送到documents2的行
您应该在使用之前初始化列表。
【参考方案1】:
在 dart 中,我们这样定义列表和映射:
static List<Map<String, dynamic>> reservations = [];
static Map<String, dynamic> documents = ;
List<QueryDocumentSnapshot> documents2 = [];
如果您想了解更多信息,请阅读 this topic 了解 dart 中的列表。
地图大多相同。
【讨论】:
以上是关于错误:flutter/lib/ui/ui_dart_state.cc(177) 未处理的异常:NoSuchMethodError:方法“插入”在 null 上调用的主要内容,如果未能解决你的问题,请参考以下文章
Pig 安装错误:错误 pig.Main:错误 2998:未处理的内部错误