如何解决未处理的异常:NoSuchMethodError:在null上调用了setter'dateTime ='

时间:2020-09-27 17:44:49

标签: flutter null

现在,我正在尝试将DB Shared Pref更改为SqlLite,并且正在测试SqlLite DB。

TodoList信息将存储在SqlLite DB中。

因此,“待办事项列表”的模型如下。

     class TodoModel {
      int dateTime;
      String todo;
      int isDone;
    
      todoModelMap() {
        var mapping =  Map<String, dynamic>();
        mapping['dateTime'] = this.dateTime;
        mapping['todo'] = this.todo;
        mapping['isDone'] = this.isDone;
    
        return mapping;
      }
    }

问题是我试图将模型存储在todoList提供程序文件中,但是编译器调用错误dateTime为空

addTodoList(DateTime dateTime, TextEditingController eventController) async {
    
        if(eventController.text.isEmpty) { return; }
        else {
          if (events[dateTime] != null) {

            ----------------------------------------------------
            todoModel.dateTime = dateTime.millisecondsSinceEpoch;
            // I used milisecondsSinceEpoch to save DateTime as int.
            todoModel.todo = eventController.text;
            todoModel.isDone = 0;
            var result = await todoService.saveTodo(todoModel);
            print(result);
            ---------------------------------------------------
    
            events[dateTime].add(toMap(eventController));
          } else {
            events[dateTime] = [toMap(eventController)];
          }
    
          eventController.clear();
          save();
          eventController.clear();
          notifyListeners();
        }
      }

我找不到为什么dateTime为null的原因,因为这里的dateTime以int格式打印得很好。

onPressed: (){
                    print(controller.selectedDay.millisecondsSinceEpoch);
                    todoListHandler.addTodoList(controller.selectedDay, eventController);
                    Navigator.pop(context);
                  },
                )

++我想知道此错误的解决方法

W / System.err(16005):java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int android.content.ClipData.getItemCount()'

当凸起按钮为onPressed时,会发生此错误。 但存储数据没有问题

//这是我的完整错误代码

    W/System.err(16005): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.ClipData.getItemCount()' on a null object reference
    W/System.err(16005):    at android.os.Parcel.readException(Parcel.java:1626)
    W/System.err(16005):    at android.os.Parcel.readException(Parcel.java:1573)
    W/System.err(16005):    at android.content.IClipboard$Stub$Proxy.getPrimaryClip(IClipboard.java:197)
    W/System.err(16005):    at android.content.ClipboardManager.getPrimaryClip(ClipboardManager.java:247)
    W/System.err(16005):    at io.flutter.plugin.platform.PlatformPlugin.getClipboardData(PlatformPlugin.java:274)
    W/System.err(16005):    at io.flutter.plugin.platform.PlatformPlugin.access$700(PlatformPlugin.java:24)
    W/System.err(16005):    at io.flutter.plugin.platform.PlatformPlugin$1.getClipboardData(PlatformPlugin.java:81)
    W/System.err(16005):    at io.flutter.embedding.engine.systemchannels.PlatformChannel$1.onMethodCall(PlatformChannel.java:141)
    W/System.err(16005):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:230)
    W/System.err(16005):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
    W/System.err(16005):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
    W/System.err(16005):    at android.os.MessageQueue.nativePollOnce(Native Method)
    W/System.err(16005):    at android.os.MessageQueue.next(MessageQueue.java:323)
    W/System.err(16005):    at android.os.Looper.loop(Looper.java:143)
    W/System.err(16005):    at android.app.ActivityThread.main(ActivityThread.java:7225)
    W/System.err(16005):    at java.lang.reflect.Method.invoke(Native Method)
    W/System.err(16005):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    W/System.err(16005):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    D/ViewRootImpl(16005): ViewPostImeInputStage processPointer 0
    D/ViewRootImpl(16005): ViewPostImeInputStage processPointer 1
    I/flutter (16005): 1601380800000
    E/flutter (16005): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The setter 'dateTime=' was called on null.
    E/flutter (16005): Receiver: null
    E/flutter (16005): Tried calling: dateTime=1601380800000
    E/flutter (16005): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
    E/flutter (16005): #1      TodoListHandler.addTodoList (package:take_a_note_project/todoList/todoList_handler.dart:57:19)
    E/flutter (16005): #2      _TodoListState._showAddDialog.<anonymous closure>.<anonymous closure> (package:take_a_note_project/todoList/todoList.dart:134:33)
    E/flutter (16005): #3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19)
    E/flutter (16005): #4      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1098:38)
    E/flutter (16005): #5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
    E/flutter (16005): #6      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
    E/flutter (16005): #7      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
    E/flutter (16005): #8      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:219:7)
    E/flutter (16005): #9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:477:9)
    E/flutter (16005): #10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:78:12)
    E/flutter (16005): #11     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:124:9)
    E/flutter (16005): #12     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
    E/flutter (16005): #13     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:122:18)
    E/flutter (16005): #14     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:108:7)
    E/flutter (16005): #15     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:220:19)
    E/flutter (16005): #16     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
    E/flutter (16005): #17     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
    E/flutter (16005): #18     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
    E/flutter (16005): #19     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
    E/flutter (16005): #20     _rootRunUnary (dart:async/zone.dart:1206:13)
    E/flutter (16005): #21     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
    E/flutter (16005): #22     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
    E/flutter (16005): #23     _invoke1 (dart:ui/hooks.dart:267:10)
    E/flutter (16005): #24     _dispatchPointerDataPacket (dart:ui/hooks.dart:176:5)

1 个答案:

答案 0 :(得分:1)

您必须首先创建TodoModel的对象,因为没有创建对象,该对象为null,然后还要尝试分配它。 那么您可以提供它的价值。

TodoModel _todoModel=new TodoModel();
_todoModel.dateTime = dateTime.millisecondsSinceEpoch;
_todoModel.todo = eventController.text;
_todoModel.isDone = 0;
var result = await todoService.saveTodo(_todoModel);

对于第二个错误java.lang.NullPointerException,您可以参考此https://github.com/flutter/flutter/issues/24757