这是一个颤抖的问题NoSuchMethodError

时间:2020-08-26 10:43:57

标签: flutter dart

我想通过API创建商品,因此我使此函数调用了createoffre函数下方的API

Future<dynamic> createOffre(String type , String title , int categorie , bool financial_contrib,
         bool emploi , int niveau , int poste , int profil , int sexe , int sport) async {
       Response response = await this
           .dio
           .post(Uri.encodeFull(this.appConfig.baseUrl + "/node"),
           data: {
             "type": type,
             "title": [
               {
                 "value": title
               }
             ],
             "field_categorie": [
               {
                 "target_id": categorie
               }
             ],
             "field_financial_contrib": [
               {
                 "value": financial_contrib
               }
             ],
             "field_emploi_possible": [
               {
                 "value": emploi
               }
             ],
             "field_niveau": [
               {
                 "target_id": niveau
               }
             ],
             "field_postes": [
               {
                 "target_id": poste
               }
             ],
             "field_profil_recherche": [
               {
                 "target_id": profil
               }
             ],
             "field_sexe": [
               {
                 "target_id": sexe
               }
             ],
             "field_sport": [
               {
                 "target_id": sport
               }
             ]
           },
    
       );
    
       if (response.statusCode == 200){
         print("done");
         return true;
       }else{
         print("error");
    
         return false;
       }
    
       /*print(this
           .cookieJar
           .loadForRequest(Uri.parse(this.appConfig.baseUrl + "/node")));
    
       print("Response ${response.data}");*/
    
       //return response;
     }

///////////////////////////////////////////////// ///////////////////////////////////////////////////// 所以当我在AddOffer文件中进行函数调用时

您可以在下面找到函数调用的代码

onPressed: () async {
                                if (_formKey.currentState.validate()) {
                                  _formKey.currentState.save();


                                  final dynamic response =
                                  await offreManger.createOffre(
                                      "offre_club",
                                      _controllerTitre.text,
                                      int.parse(selectedIdcategorie),
                                      true,
                                      true,
                                      int.parse(selectedIdlevel),
                                      1, 1, int.parse(selectedIdSexe),
                                      int.parse(selectedIdSport)
                                  );
                                  if (response != false) {


                                    Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                        builder: (context) {
                                          return ProfileObject();
                                        },
                                        fullscreenDialog: false,
                                      ),
                                    );
                                  } else {

                                    return AwesomeDialog(
                                        context: context,
                                        dialogType: DialogType.ERROR,
                                        animType: AnimType.SCALE,
                                        headerAnimationLoop: false,
                                        title: 'Erreur',
                                        desc: "verifier vos identifiants",
                                        btnOkOnPress: () {},
                                        btnOkColor: Colors.red)
                                        .show();
                                  }
                                  return;
                                }


                              },

///////////////////////////////////////////////// //////////////////////////////////////////////////// /

当我验证表格并按下按钮以执行功能时,出现以下错误 那我该怎么办

E/flutter (24391): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The method 'createOffre' was called on null.
E/flutter (24391): Receiver: null
E/flutter (24391): Tried calling: createOffre("offre_club", "ee", 230, true, true, 131, 1, 1, 130, 1)
E/flutter (24391): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
E/flutter (24391): #1      _AjouterOffreFormState.build.<anonymous closure> (package:monmercato_app/Widgets/OffreWidget/AjouterOffreForme.dart:207:53)
E/flutter (24391): #2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19)
E/flutter (24391): #3      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1098:38)
E/flutter (24391): #4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
E/flutter (24391): #5      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
E/flutter (24391): #6      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
E/flutter (24391): #7      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:219:7)
E/flutter (24391): #8      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:477:9)
E/flutter (24391): #9      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:78:12)
E/flutter (24391): #10     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:124:9)
E/flutter (24391): #11     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
E/flutter (24391): #12     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:122:18)
E/flutter (24391): #13     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:108:7)
E/flutter (24391): #14     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:220:19)
E/flutter (24391): #15     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
E/flutter (24391): #16     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
E/flutter (24391): #17     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
E/flutter (24391): #18     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
E/flutter (24391): #19     _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter (24391): #20     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (24391): #21     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (24391): #22     _invoke1 (dart:ui/hooks.dart:267:10)
E/flutter (24391): #23     _dispatchPointerDataPacket (dart:ui/hooks.dart:176:5)
E/flutter (24391): 

1 个答案:

答案 0 :(得分:0)

方法'createOffre'在null上调用。

您的变量offreMangernull。一定不是。您必须在某个地方进行初始化,无论是在声明的地方,还是在initState或任何您想执行的地方,但是必须在调用它的函数之前将其初始化为某种东西。