Flutter:方法'/'在null上被调用。接收方:null尝试调用:/(1080.0)

时间:2020-03-22 16:59:39

标签: flutter dart flutter-layout

由于使用 Media Query (媒体查询)设置响应式用户界面,因此出现此错误。下面是我在终端中遇到的错误。

enter image description here

我不知道哪个方法调用根据Dart返回null。因为我正在通过“升高按钮”导航到新屏幕,并且在我在按钮的 onPressed 上调用的 Widget / Class的构造函数上显示了此错误,

这是我从那里导航到新的医生登录屏幕的班级代码:

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:medkit/animations/fadeAnimation.dart';
import 'package:medkit/animations/bottomAnimation.dart';
import 'package:medkit/doctor/doctorLogin.dart';
import 'package:medkit/doctor/doctorPanel.dart';
import 'aboutUs.dart';

class UserType extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;

    Future<bool> _onWillPop() async {
      return (await showDialog(
        context: context,
        builder: (context) => new AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12)
          ),
          title: new Text(
            "Exit Application",
            style: TextStyle(fontWeight: FontWeight.bold),
          ),
          content: new Text("Are You Sure?"),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            FlatButton(
              shape: StadiumBorder(),
              color: Colors.white,
              child: new Text(
                "Close",
                style: TextStyle(color: Colors.blue),
              ),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            FlatButton(
              shape: StadiumBorder(),
              color: Colors.white,
              child: new Text(
                "Yes",
                style: TextStyle(color: Colors.red),
              ),
              onPressed: () {
                exit(0);
              },
            ),
          ],
        ),
      )) ??
          false;
    }

    return WillPopScope(
      onWillPop: _onWillPop,
      child: Scaffold(
        body: SafeArea(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: width * 0.04),
            child: Column(
              children: <Widget>[
                SizedBox(
                  height: height * 0.08,
                ),
                FadeAnimation(
                  0.3,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text(
                        'Category',
                        style: TextStyle(color: Colors.black, fontSize: 30),
                      ),
                      GestureDetector(
                        onTap: () => _exitAlert(context),
                        child: Icon(
                          Icons.exit_to_app,
                          size: height * 0.04,
                        ),
                      )
                    ],
                  ),
                ),
                SizedBox(height: height * 0.09),
                Column(
                  children: <Widget>[
                    FadeAnimation(
                      0.4,
                      CircleAvatar(
                        backgroundColor: Colors.black.withOpacity(0.2),
                        radius: height * 0.075,
                        child: Image(image: AssetImage("assets/doctor.png")),
                      ),
                    ),
                    WidgetAnimator(patDocBtn('Doctor', context)),
                    SizedBox(
                      height: height * 0.1,
                    ),
                    FadeAnimation(
                      0.4,
                      CircleAvatar(
                        backgroundColor: Colors.black.withOpacity(0.2),
                        radius: height * 0.075,
                        child: Image(image: AssetImage("assets/patient.png")),
                      ),
                    ),
                    WidgetAnimator(patDocBtn('Patient', context)),
                    SizedBox(height: height * 0.13,),
                    GestureDetector(
                      onTap: () => Navigator.push(context, new MaterialPageRoute(builder: (context) => AboutUs())),
                      child: Column(
                        children: <Widget>[
                          Text('Version', style: TextStyle(fontWeight: FontWeight.bold),),
                          Text('V 0.1', style: TextStyle(fontSize: 12),)
                        ],
                      ),
                    ),
                    SizedBox(height: 5,)
                  ],
                )
              ],
            ),
          ),
        ),
      ),
    );

  }

  Widget patDocBtn(String categoryText, context) {
    return Container(
      width: MediaQuery.of(context).size.width * 0.5,
      child: RaisedButton(
        onPressed: () {
          if (categoryText == 'Doctor') {
            Navigator.pushNamed(context, 'DoctorLogin');
          } else {
            Navigator.pushNamed(context, 'PatientLogin');
          }
        },
        color: Colors.white,
        child: Text("I am " + categoryText),
        shape: StadiumBorder(),
      ),
    );
  }

  _exitAlert(context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12)
          ),
          title: new Text(
            "Exit Application",
            style: TextStyle(fontWeight: FontWeight.bold),
          ),
          content: new Text("Are You Sure?"),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            FlatButton(
              color: Colors.white,
              child: new Text(
                "Close",
                style: TextStyle(color: Colors.blue),
              ),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            FlatButton(
              color: Colors.white,
              child: new Text(
                "Yes",
                style: TextStyle(color: Colors.red),
              ),
              onPressed: () {
                exit(0);
              },
            ),
          ],
        );
      },
    );
  }
}

这是医生的登录课程代码

import 'dart:math';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:medkit/animations/fadeAnimation.dart';
import 'package:medkit/animations/bottomAnimation.dart';
import 'package:medkit/doctor/doctorPanel.dart';
import 'package:medkit/otherWidgetsAndScreen/backBtn.dart';
import 'package:medkit/otherWidgetsAndScreen/imageAvatar.dart';
import 'package:toast/toast.dart';

class DoctorLogin extends StatefulWidget {
  @override
  _DoctorLoginState createState() => _DoctorLoginState();
}

class _DoctorLoginState extends State<DoctorLogin> {
  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
  final GoogleSignIn _googleSignIn = new GoogleSignIn();

  Future<FirebaseUser> _signIn(BuildContext context) async {
    final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
    final GoogleSignInAuthentication googleAuth =
        await googleUser.authentication;

    final AuthCredential credential = GoogleAuthProvider.getCredential(
        idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);

    FirebaseUser userDetails =
        await _firebaseAuth.signInWithCredential(credential);
    ProviderDoctorDetails providerInfo =
        new ProviderDoctorDetails(userDetails.providerId);

    List<ProviderDoctorDetails> providerData =
        new List<ProviderDoctorDetails>();
    providerData.add(providerInfo);

    DoctorDetails details = new DoctorDetails(
      userDetails.providerId,
      userDetails.displayName,
      userDetails.photoUrl,
      userDetails.email,
      providerData,
    );

    Navigator.push(
        context,
        new MaterialPageRoute(
            builder: (context) => DoctorPanel(
                  detailsUser: details,
                )));

    return userDetails;
  }

  @override
  Widget build(BuildContext context) {

    final nameTextFieldController = TextEditingController();
    final nameTextField = TextField(
      keyboardType: TextInputType.text,
      autofocus: false,
      maxLength: 30,
      textInputAction: TextInputAction.next,
      onSubmitted: (_) => FocusScope.of(context).nextFocus(),
      controller: nameTextFieldController,
      decoration: InputDecoration(
          fillColor: Colors.black.withOpacity(0.07),
          filled: true,
          labelText: 'Enter Name',
          prefixIcon: Icon(Icons.person),
          border: OutlineInputBorder(
              borderRadius: const BorderRadius.all(const Radius.circular(20)))),
    );

    final phNumberTextController = TextEditingController();
    final phoneTextField = TextField(
      keyboardType: TextInputType.phone,
      autofocus: false,
      maxLength: 11,
      controller: phNumberTextController,
      textInputAction: TextInputAction.next,
      onSubmitted: (_) => FocusScope.of(context).nextFocus(),
      decoration: InputDecoration(
        filled: true,
        fillColor: Colors.black.withOpacity(0.07),
        labelText: 'Enter Number',
        prefixIcon: Icon(Icons.phone),
        border: new OutlineInputBorder(
          borderRadius: const BorderRadius.all(const Radius.circular(20)),
        ),
      ),
    );

    final cnicTextController = TextEditingController();
    final cnicTextField = TextFormField(
      keyboardType: TextInputType.number,
      autofocus: false,
      maxLength: 13,
      controller: cnicTextController,
      decoration: InputDecoration(
          filled: true,
          fillColor: Colors.black.withOpacity(0.07),
          labelText: 'Enter CNIC',
          prefixIcon: Icon(Icons.card_membership),
          border: OutlineInputBorder(borderRadius: BorderRadius.circular(20))),
    );

    final double width = MediaQuery.of(context).size.width;
    final double height = MediaQuery.of(context).size.height;
    return GestureDetector(
      onTap: () {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus) {
          currentFocus.unfocus();
        }
      },
      child: Scaffold(
          resizeToAvoidBottomPadding: false,
          body: SafeArea(
            child: Container(
              width: width,
              height: height,
              child: Stack(
                children: <Widget>[
                  ImageAvatar(
                    assetImage: 'assets/bigDoc.png',
                  ),
                  Container(
                    width: width,
                    height: height,
                    margin: EdgeInsets.fromLTRB(width * 0.03, 0, width * 0.03, 0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        BackBtn(),
                        SizedBox(height: height * 0.05,),
                        Text(
                          "\t\tLogin",
                          style: GoogleFonts.abel(
                              fontSize: ScreenUtil.instance.setSp(35),
                              fontWeight: FontWeight.bold),
                        ),
                        SizedBox(
                          height: height * 0.05,
                        ),
                        WidgetAnimator(nameTextField),
                        WidgetAnimator(phoneTextField),
                        WidgetAnimator(cnicTextField),
                        SizedBox(height: height * 0.01,),
                        FadeAnimation(
                          1.5,
                          SizedBox(
                            width: width,
                            height: height * 0.07,
                            child: RaisedButton(
                              color: Colors.white,
                              shape: StadiumBorder(),
                              onPressed: () {
                                if (nameTextFieldController.text != "" ||
                                    phNumberTextController.text != "" ||
                                    cnicTextController.text != "") {
                                  Firestore.instance
                                      .collection('doctorInfo')
                                      .document(nameTextFieldController.text)
                                      .setData({
                                    'cnic': cnicTextController.text,
                                    'phoneNumber': phNumberTextController.text,
                                  });
                                  _signIn(context)
                                      .then((FirebaseUser user) =>
                                          print('Gmail Logged In'))
                                      .catchError((e) => print(e));
                                  nameTextFieldController.clear();
                                  cnicTextController.clear();
                                  phNumberTextController.clear();
                                } else {
                                  Toast.show('Fields Cannot be Empty!', context,
                                      backgroundColor: Colors.red,
                                      gravity: Toast.CENTER,
                                      duration: Toast.LENGTH_SHORT);
                                }
                              },
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  Image(
                                    image: AssetImage('assets/google.png'),
                                    height: ScreenUtil.instance.setHeight(35),
                                  ),
                                  SizedBox(width: ScreenUtil.instance.setWidth(15)),
                                  Text(
                                    'Login',
                                    style: TextStyle(
                                        letterSpacing: 2,
                                        fontWeight: FontWeight.bold,
                                        fontSize: ScreenUtil.instance.setSp(18)),
                                  )
                                ],
                              ),
                            ),
                          ),
                        ),
                        SizedBox(height: height * 0.02,),
                        FadeAnimation(
                          2,
                          Text(
                            'You Will be asked Question regarding your Qualifications!', textAlign: TextAlign.center,
                            style: TextStyle(color: Colors.black.withOpacity(0.5),),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          )),
    );
  }
}

class DoctorDetails {
  final String providerDetails;
  final String userName;
  final String photoUrl;
  final String userEmail;
  final List<ProviderDoctorDetails> providerData;

  DoctorDetails(this.providerDetails, this.userName, this.photoUrl,
      this.userEmail, this.providerData);
}

class ProviderDoctorDetails {
  ProviderDoctorDetails(this.providerDetails);

  final String providerDetails;
}

2 个答案:

答案 0 :(得分:0)

解决方案

我同时使用Media Query和第三方依赖。我刚刚在代码中删除了Dependency及其用法,就解决了

结论 可能您无法同时使用Media Query和第三方依赖项来使您的应用程序具有响应性

答案 1 :(得分:0)

您的pushNamed路由,路由名称以'/'开头?您是否将其声明为“ / NAMEOFTHEROUTE”? 如果是,请使用您的pushNamed作为“ / NAMEOFTHEROUTE”。