如何解决“该方法在null上被调用”的错误?

时间:2019-08-31 10:27:34

标签: flutter dart

我正在尝试创建形状不正确的对象并将其添加到数据库中。

我在调用函数之前打印了所有值,但都不为空。

这是我得到错误的地方

Exercise ex=new Exercise(name: name,description: description,type:inputType,mainMuscle: inputMainMuscle,equipment:equip,userCreated: 1);

                 _databaseHelper.addExercise(ex).then((exerciseid){
                   if(exerciseid!=0){
                     Navigator.pop(context);
                   }
                 }

这是我的功能:

Future<int> addExercise(Exercise exercise) async{
    var db = await _getDatabase();
    var sonuc = await db.insert("exercises", exercise.toMap());
    return sonuc;
 }

这是我的建设者:

Exercise({this.name,this.description='Henüz bir açıklama eklenmemiş ☹️',this.type, this.equipment,this.mainMuscle='Undefined',this.userCreated});

flutter: ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
flutter: The following NoSuchMethodError was thrown while handling a gesture:
flutter: The method 'addExercise' was called on null.
flutter: Receiver: null
flutter: Tried calling: addExercise(Instance of 'Exercise')
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
flutter: #1      _CreateExerciseState.build.<anonymous closure> (package:gymapp/view/viewmodels/createexercise.dart:99:34)
flutter: #2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:635:14)
flutter: #3      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:710:32)
flutter: #4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
flutter: #5      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
flutter: #6      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
flutter: #7      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:467:9)
flutter: #8      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
flutter: #9      PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
flutter: #10     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
flutter: #11     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
flutter: #12     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
flutter: #13     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
flutter: #14     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
flutter: #18     _invoke1 (dart:ui/hooks.dart:263:10)
flutter: #19     _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)
flutter: (elided 3 frames from package dart:async)
flutter:
flutter: Handler: "onTap"
flutter: Recognizer:
flutter:   TapGestureRecognizer#225e0
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════

0 个答案:

没有答案