NoSuchMethodError:方法'+'在null上被调用。接收方:null尝试调用:+(“”)

时间:2020-02-27 05:06:19

标签: flutter

我正在成功地从服务器获取数据。但是在我的应用程序中,我想一次性登录,以我登录到应用程序后可以成功登录,但是当我从内存中清除我的应用程序然后再次打开它然后首先显示启动屏幕时,将显示此错误:"No Such Method Error: The method '+' was called on null. Receiver: null Tried calling: +(" ")."我从堆栈溢出尝试了很多解决方案,但是我无法获得确切的解决方案。

登录

import 'dart:io';
import 'package:bar_app/Home.dart';
import 'package:bar_app/Methods/ALLResourceMethods.dart';
import 'package:bar_app/Models/ALLModels.dart';
import 'package:bar_app/SignUp1.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  runApp(Login());
}

class Login extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<Login> {

  TextEditingController getEmail = TextEditingController();
  TextEditingController getPassword = TextEditingController();

  Future<bool> _onWillPop() {
    exit(0);
  }
  @override
  void initState() {
    SystemChrome.setEnabledSystemUIOverlays([]);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onWillPop,
      child: Scaffold(
        body: Center(
          child: SingleChildScrollView(
            child: Center(
              child: Container(
                child: Column(
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.only(top: 50),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Image(
                            image: new AssetImage("images/logoorg.png"),
                            height: 200,
                            width: 200,
                          ),
                          Text(
                            'Welcome back.',
                            style: TextStyle(
                              fontFamily: 'Poppins',
                              fontSize: 18,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      height: MediaQuery.of(context).size.height / 2,
                      width: MediaQuery.of(context).size.width,
                      padding: EdgeInsets.only(top: 62),
                      child: Column(
                        children: <Widget>[
                          Container(
                            width: MediaQuery.of(context).size.width / 1.3,
                            height: 50,
                            padding: EdgeInsets.only(
                                top: 4, left: 16, right: 16, bottom: 4),
                            decoration:
                                BoxDecoration(color: Colors.white, boxShadow: [
                              BoxShadow(color: Colors.black12, blurRadius: 5)
                            ]),
                            child: TextField(
                              controller: getEmail,
                              autofocus: true,
                              decoration: InputDecoration(
                                  border: InputBorder.none, hintText: 'Email'),
                            ),
                          ),
                          Container(
                            width: MediaQuery.of(context).size.width / 1.3,
                            height: 50,
                            margin: EdgeInsets.only(top: 20, bottom: 20),
                            padding: EdgeInsets.only(
                                top: 4, left: 16, right: 16, bottom: 4),
                            decoration:
                                BoxDecoration(color: Colors.white, boxShadow: [
                              BoxShadow(color: Colors.black12, blurRadius: 5)
                            ]),
                            child: TextField(
                              controller: getPassword,
                              decoration: InputDecoration(
                                  border: InputBorder.none,
                                  hintText: 'Password'),
                              obscureText: true,
                            ),
                          ),
                          Container(
                            height: 50,
                            width: MediaQuery.of(context).size.width / 1.3,
                            child: RaisedButton(
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(25.0),
                              ),
                              child: Text(
                                'Login',
                                style: TextStyle(fontFamily: 'Poppins'),
                              ),
                              textColor: Colors.white,
                              onPressed: () {
                                setState(() {
                                 Login1.SignIn(getEmail.toString(),getPassword.toString(), context);
                                });
                              },
                              color: Colors.orange,
                              padding: EdgeInsets.symmetric(
                                  horizontal: 8.0,
                                  vertical: 5.0), // gives padding to the button
                            ),
                          ),
                          Align(
                            alignment: Alignment.center,
                            child: Padding(
                              padding: const EdgeInsets.only(top: 20),
                              child: new GestureDetector(
                                onTap: () {
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => SignUp1()),
                                  );
                                },
                                child: new Text(
                                  "SIGN UP",
                                  style: TextStyle(
                                      fontFamily: 'Poppins',
                                      fontWeight: FontWeight.bold,
                                      fontSize: 15),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

API Calling  method here I set shared preference data

class Login1 {
  static Future<LoginModel> SignIn(
      String email, String password, context) async {
    var url = 'https://bruyou.project-demo.info/api/V1/customer/login';
    Map data = {
      "email": email,
      "password": password,
    };
    var body = json.encode(data);
    var response = await http.post(url,
        headers: {"Content-Type": "application/json"}, body: body);
    final responseJson = json.decode(response.body);

    if (responseJson["status"] == "error") {
      Navigator.of(context).pushAndRemoveUntil(
          MaterialPageRoute(builder: (context) => Login()),
          (Route<dynamic> route) => false);
    } else {
      var dataModel = LoginModel.fromJson(responseJson);
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setString("id", dataModel.successData.id.toString());
      prefs.setString("token", dataModel.successData.rememberToken);
      prefs.setString("fn", dataModel.successData.firstName);
      prefs.setString("ln", dataModel.successData.lastName);
      prefs.setString("img", dataModel.successData.profileImage);
      prefs.setString("pn", dataModel.successData.phoneNumber);
      prefs.setString("isTrue", "true");
      Navigator.of(context).push(
        MaterialPageRoute(
          builder: (context) => Home(
            pn: dataModel.successData.phoneNumber,
            img: dataModel.successData.profileImage,
            fn: dataModel.successData.firstName,
          ),
        ),
      );
    }
  }
}

Splash screen code

import 'package:bar_app/Home.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'Login.dart';
import 'dart:async';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext contextP) {
    return MaterialApp(debugShowCheckedModeBanner: false, home: splash());
  }
}

class splash extends StatefulWidget {
  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<splash> {
  var CheckToken = null;
  var Check = null;

  oneTime() async{
    SharedPreferences Preference = await SharedPreferences.getInstance();
    var Check = Preference.getString("isTrue");
    var fn= Preference.getString("fn");
    //var ln= Preference.getString("ln");
    var img= Preference.getString("img");
    var pn= Preference.getString("pn");
    print("fnnnn"+fn);
    if (Check == "true") {
      Timer(
        Duration(seconds: 5),
        () => Navigator.of(context).push(
          MaterialPageRoute(builder: (BuildContext context) => Home(fn: fn,img: img,pn:pn)),
        ),
      );
    } else {
      Timer(
        Duration(seconds: 5),
        () => Navigator.of(context).push(
          MaterialPageRoute(builder: (BuildContext context) => Login()),
        ),
      );
    }
  }

  @override
  void initState() {
    oneTime();
    SystemChrome.setEnabledSystemUIOverlays([]);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(8.0),
          child: Center(
            child: Container(
              child: Center(
                child: Image(
                  image: new AssetImage("images/photofunky.gif"),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您的问题在这一行

 print("fnnnn"+fn);

fn为null,因此它调用null上的缩略词,该剂量词支持字符串缩略词