未处理的异常:NoSuchMethodError:在null上调用了方法'split'

时间:2019-08-06 19:28:59

标签: firebase flutter

这是一个简单的联系人应用程序,可以添加查看和编辑个人的联系人详细信息。我无法完全理解这里的错误。

 class ViewContact extends StatefulWidget {
  final String id;
  ViewContact(this.id);
  @override
  _ViewContactState createState() => _ViewContactState(this.id);
}

class _ViewContactState extends State<ViewContact> {  
  DatabaseReference _databaseReference = FirebaseDatabase.instance.reference();
   String mascot = "https://learncodeonline.in/mascot.png";
   String id; 
  _ViewContactState(id);
  Contact _contact;                                   
  bool isLoading = true;

  getContact(id) async {
     _databaseReference.child(id).onValue.listen ((event){
      setState(()  {
        print(event.snapshot);
        _contact =   Contact.fromSnapshot(event.snapshot);
        isLoading = false;
        print(_contact.lastName);
      });
    });
  }
  @override
  void initState(){
    super.initState();
    this.getContact(id);
  }

  callAction(String number) async{
    String url = 'tel: $number';
    if (await canLaunch(url)){
      await launch(url);
    }
    else {
      throw 'Could call $number';
    }
  }
  smsAction(String number) async{
    String url ='sms: $number';
    if (await canLaunch(url)){
      await launch(url);
    }
    else {
      throw 'lol';
    }
  }
  navigateToLastScreen(){
    Navigator.pop(context);
  }
  navigateToEditScreen(id){
    Navigator.push(context, MaterialPageRoute(builder: (context){
      return EditContact();
    }));
  }

下面显示的错误。错误可能是由于id变量引起的吗? (对Flutter来说是个新手)。

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'split' was called on null.
E/flutter (12369): Receiver: null
E/flutter (12369): Tried calling: split("/")
E/flutter (12369): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
E/flutter (12369): #1      DatabaseReference.child (package:firebase_database/src/database_reference.dart:24:58)
E/flutter (12369): #2      _ViewContactState.getContact (package:firebase_authentication/screens/view_contact.dart:24:25)
E/flutter (12369): <asynchronous suspension>
E/flutter (12369): #3      _ViewContactState.initState (package:firebase_authentication/screens/view_contact.dart:36:10)
E/flutter (12369): #4      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4033:58)
E/flutter (12369): #5      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5)
E/flutter (12369): #6      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (12369): #7      Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (12369): #8      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14)
E/flutter (12369): #9      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (12369): #10     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (12369): #11     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16)
E/flutter (12369): #12     Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
E/flutter (12369): #13     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5)
E/flutter (12369): #14     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5)
E/flutter (12369): #15     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (12369): #16     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (12369): #17     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14)
E/flutter (12369): #18     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (12369): #19     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (12369): #20     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14)
E/flutter (12369): #21     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (12369): #22     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (12369): #23     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14)
E/flutter (12369): #24     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (12369): #25     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (12369): #26     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14)
E/flutter (12369): #27     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (12369): #28     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (12369): #29     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16)
E/flutter (12369): #30     Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
E/flutter (12369): #31     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5)
E/flutter (12369): #32     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4053:11)
E/flutter (12369): #33     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5)
E/flutter (12369): #34     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (12369): #35     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (12369): #36     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16)
E/flutter (12369): #37     Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
E/flutter (12369): #38     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5)
E/flutter (12369): #39     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5)
E/flutter (12369): #40     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (12369): #41     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (12369): #42     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16)
E/flutter (12369): #43     Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
E/flutter (12369): #44     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart

这是我面临的堆栈溢出错误。与上一个错误一样,我也无法弄清楚这个错误:(

I/flutter ( 4594): The following StackOverflowError was thrown building ViewContact(dirty, state:
I/flutter ( 4594): _ViewContactState#4e21c):
I/flutter ( 4594): Stack Overflow
I/flutter ( 4594):
I/flutter ( 4594): When the exception was thrown, this was the stack:
I/flutter ( 4594): #0      Contact.email (package:firebase_authentication/model/contact.dart:25:4)
I/flutter ( 4594): #1      Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #2      Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #3      Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #4      Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #5      Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #6      Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #7      Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #8      Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #9      Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #10     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #11     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #12     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #13     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #14     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #15     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #16     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #17     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #18     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #19     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #20     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #21     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #22     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #23     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #24     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #25     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #26     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #27     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #28     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #29     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #30     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #31     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #32     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #33     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #34     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #35     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #36     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #37     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #38     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #39     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #40     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #41     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #42     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #43     Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): ...
I/flutter ( 4594): ...
I/flutter ( 4594): #59478  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59479  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59480  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59481  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59482  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59483  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59484  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59485  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59486  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59487  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59488  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59489  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59490  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59491  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59492  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59493  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59494  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59495  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59496  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59497  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59498  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59499  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59500  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59501  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59502  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59503  Contact.email (package:firebase_authentication/model/contact.dart:25:29)
I/flutter ( 4594): #59504  _ViewContactState.build (package:firebase_authentication/screens/view_contact.dart:179:40)
I/flutter ( 4594): #59505  StatefulElement.build (package:flutter/src/widgets/framework.dart:4012:27)
I/flutter ( 4594): #59506  ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3924:15)
I/flutter ( 4594): #59507  Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
I/flutter ( 4594): #59508  BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2340:33)
I/flutter ( 4594): #59509  _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:700:20)
I/flutter ( 4594): #59510  _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:285:5)
I/flutter ( 4594): #59511  _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1016:15)
I/flutter ( 4594): #59512  _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:958:9)
I/flutter ( 4594): #59513  _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:874:5)
I/flutter ( 4594): #59517  _invoke (dart:ui/hooks.dart:236:10)
I/flutter ( 4594): #59518  _drawFrame (dart:ui/hooks.dart:194:3)
I/flutter ( 4594): (elided 3 frames from package dart:async)

================================================ ================================ 这是与“查看联系人”状态关联的窗口小部件。

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Add Contact"),
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          onPressed: (){navigateToLastScreen(context);},
        ),
      ),
      body: Container(
        padding: EdgeInsets.all(20.0),
        child: ListView(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(top: 20.0),
              child: GestureDetector(
                onTap: (){
                  this.pickImage();
                },
                child: Center(
                  child: Container(
                    width: 130.0,
                    height: 130.0,

                    decoration: BoxDecoration(
                      border: Border.all(color: Colors.white, width: 4.0),
                      shape: BoxShape.circle,

                      image: DecorationImage(
                        fit: BoxFit.fill,
                        image: _photoUrl=="empty"? NetworkImage("https://i.imgur.com/BoN9kdC.png"): NetworkImage(_photoUrl),
                      ),
                    ),
                  ),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 40.0),
              child: TextField(

                onChanged: (value){
                  setState(() {
                   _firstName = value; 
                  });
                },
                decoration: InputDecoration(
                  labelText: 'First Name',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 20.0),
              child: TextField(

                onChanged: (value){
                  setState(() {
                   _lastName = value; 
                  });
                },
                decoration: InputDecoration(
                  labelText: 'Last Name',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 20.0),
              child: TextField(
                onChanged: (value){
                  setState(() {
                   _email = value; 
                  });
                },
                decoration: InputDecoration(
                  labelText: 'E-mail',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 20.0),
              child: TextField(

                onChanged: (value){
                  setState(() {
                   _phone = value; 
                  });
                },
                decoration: InputDecoration(
                  labelText: 'Phone',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 20.0),
              child: TextField(

                onChanged: (value){
                  setState(() {
                   _address = value; 
                  });
                },
                decoration: InputDecoration(
                  labelText: 'Address',

                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                ),
              ),
            ),
            //save the shit
            Container(
              padding: EdgeInsets.only(top: 20.0),
              child: RaisedButton(
                color: Colors.teal,
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
                padding: EdgeInsets.fromLTRB(50, 10, 50, 10),
                onPressed: (){
                  saveContact(context);
                },
                child: Text("ADD CONTACT", style: TextStyle(color: Colors.white, fontSize: 20.0, fontWeight: FontWeight.bold),),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:1)

我相信 id为空

pip install filename.whl

在堆栈跟踪之后,此行引发异常:

E/flutter (12369): #2      _ViewContactState.getContact (package:firebase_authentication/screens/view_contact.dart:24:25)
_databaseReference.child(id).onValue.listen((event)

从database_reference.dart第19-25行开始:

E/flutter (12369): #1      DatabaseReference.child (package:firebase_database/src/database_reference.dart:24:58)

路径是您的ID,所以我会说ID为空。

  /// Gets a DatabaseReference for the location at the specified relative
  /// path. The relative path can either be a simple child key (e.g. ‘fred’) or
  /// a deeper slash-separated path (e.g. ‘fred/name/first’).
  DatabaseReference child(String path) {
    return DatabaseReference._(_database,
        (List<String>.from(_pathComponents)..addAll(path.split('/'))));
  }

您可以将断言添加到构造函数中,如果id为null,则将引发错误,然后您可以按照stacktrace查找损坏的代码。

E/flutter (12369): Receiver: null
E/flutter (12369): Tried calling: split("/")

编辑:

#1
更改此行:

class ViewContact extends StatefulWidget {
  final String id;

  const ViewContact({Key key, @required this.id})
      : assert(id != null),
        super(key: key);

  @override
  _ViewContactState createState() => _ViewContactState(this.id);
}

  _ViewContactState(id);

状态中的字段ID永远不会更新!

#2
您可以通过调用 widget.id
访问Widget类中归档的ID。 id永远不会更改,不要将其放入State构造函数。

这应该有效

_ViewContactState(this.id);

答案 1 :(得分:1)

您应该使用

 _ViewContactState(this.id);
在构造函数中

初始化ID。否则它将为null,从而导致错误。

但是您实际上并不需要传递它,因为StatefulWidget可以通过 widget 调用

来访问它。
class ViewContact extends StatefulWidget {
  final String id;
  ViewContact(this.id);
  @override
  _ViewContactState createState() => _ViewContactState();
}

class _ViewContactState extends State<ViewContact> {
  DatabaseReference _databaseReference = FirebaseDatabase.instance.reference();
  String mascot = "https://learncodeonline.in/mascot.png";

  _ViewContactState();
  Contact _contact;
  bool isLoading = true;

  getContact(id) async {
    _databaseReference.child(id).onValue.listen ((event){
      setState(()  {
        print(event.snapshot);
        _contact =   Contact.fromSnapshot(event.snapshot);
        isLoading = false;
        print(_contact.lastName);
      });
    });
  }
  @override
  void initState(){
    super.initState();
    this.getContact(widget.id);
  }