当前位置: 首页 > 知识库问答 >
问题:

错误:flutter/lib/ui/ui_dart_state.cc(177)未处理的异常:NoSuch方法错误:在空调用方法插入

宗建章
2023-03-14

你好,我是一个入门的flutter开发人员,我想用我的CloudFireStore数据库中的数据做一个列表。我不知道该怎么做。如果您对我的代码有任何帮助或建议,我们将不胜感激

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):


这里有一条错误消息。如果有人能帮我解决这个问题我会从幸福中发疯

共有1个答案

马峻
2023-03-14

在dart中,我们定义列表和地图如下:

static List<Map<String, dynamic>> reservations = [];
static Map<String, dynamic> documents = {};
List<QueryDocumentSnapshot> documents2 = [];

如果您想了解更多信息,请阅读本主题关于省道中的列表。

地图基本上是一样的。

 类似资料: