首页 > 解决方案 > firebase 与颤振应用程序没有连接

问题描述

问题是什么 ?之后加载小部件正在工作,而不是连接小部件正在运行我该如何解决这个问题

我的代码:

import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:marwa_app/component/myResponsiveLibrary.dart'; import 'package:marwa_app/component/Logo.dart'; import 'package:marwa_app/component/MyDrawer.dart'; import 'package:geolocator/geolocator.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_core/firebase_core.dart';

class Reporting extends StatefulWidget {   @override  
_ReportingState createState() => _ReportingState(); }

class _ReportingState extends State<Reporting> {   final formKey = GlobalKey<FormState>();   final scaffoldKey = GlobalKey<ScaffoldState>();

  BoxDecoration boxDecoration() {
    return BoxDecoration(
      color: MainModel().mainColor,
      borderRadius: BorderRadius.all(Radius.circular(40.0)),
      boxShadow: [
        BoxShadow(
          color: Colors.black.withOpacity(0.15),
          spreadRadius: 5,
          blurRadius: 5,
          offset: Offset(2, 2), // changes position of shadow
        ),
      ],
    );   }

  TextStyle titleStyle() {
    return TextStyle(
        color: MainModel().whiteColor,
        fontSize: MainModel().setFont(MainModel().largeFont, getWidth()));   }

  TextStyle textStyle() {
    return TextStyle(
      color: MainModel().whiteColor,
      fontSize: MainModel().setFont(MainModel().middleFont, getWidth()),
    );   }

  double getWidth() {
    return MediaQuery.of(context).size.width;   }

  final _name = TextEditingController();   final _phone = TextEditingController();   final _emType = TextEditingController();  double lat;   double lon;   bool _initialized = false;   bool _error
= false;

  void initializeFlutterFire() async {
    try {
      await Firebase.initializeApp();
      setState(() {
        _initialized = true;
      });
    } catch(e) {
      // Set `_error` state to true if Firebase initialization fails
      setState(() {
        _error = true;
      });
    }   }

  @override   void initState() {
    initializeFlutterFire();
    super.initState();   }   @override   Widget build(BuildContext context) {
    if(_error) {
      return notConnection();
    }
    if (!_initialized) {
      return Loading();
    }
    return isConnection();   }

  Widget isConnection() {
    final widthScreen = MediaQuery.of(context).size.width;
    return Scaffold(
        key: scaffoldKey,
        backgroundColor: MainModel().mainColor,
        appBar: AppBar(
          backgroundColor: Color(0xff323266),
          leading: IconButton(
              icon: Icon(
                Icons.notifications_active,
                color: MainModel().thirdColor,
              ),
              onPressed: () => {}),
          title: Logo(),
          centerTitle: true,
          actions: [
            Builder(
              builder: (context) => IconButton(
                icon: Icon(
                  Icons.menu,
                  color: MainModel().thirdColor,
                ),
                onPressed: () => Scaffold.of(context).openEndDrawer(),
                tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
              ),
            ),
          ],
        ),
        endDrawer: Drawer(
          child: MyDrawer(),
        ),
        body: ListView(
          padding: EdgeInsets.only(
              top: MainModel()
                  .setPadding(MainModel().largePadding, widthScreen)),
          children: [
            Form(
                key: formKey,
                child: Column(
                  children: [
                    Directionality(
                      textDirection: TextDirection.rtl,
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 20),
                        margin: EdgeInsets.symmetric(horizontal: 20),
                        alignment: Alignment.centerRight,
                        decoration: boxDecoration(),
                        child: TextFormField(
                          controller: _name,
                          textDirection: TextDirection.rtl,
                          autofocus: true,
                          decoration: InputDecoration(
                              border: InputBorder.none,
                              hintStyle: textStyle(),
                              hintText: 'الاسم الرباعي'),
                          validator: (value) {
                            if (value.isEmpty) {
                              return "يرجى ادخل الاسم";
                            } else {
                              return null;
                            }
                          },
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(
                          top: MainModel().setPadding(
                              MainModel().middlePadding, widthScreen)),
                    ),
                    Directionality(
                      textDirection: TextDirection.rtl,
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 20),
                        margin: EdgeInsets.symmetric(horizontal: 20),
                        alignment: Alignment.centerRight,
                        decoration: boxDecoration(),
                        child: TextFormField(
                          controller: _phone,
                          keyboardType: TextInputType.number,
                          textDirection: TextDirection.rtl,
                          autofocus: true,
                          decoration: InputDecoration(
                              border: InputBorder.none,
                              hintStyle: textStyle(),
                              hintText: 'رقم الهاتف'),
                          validator: (value) {
                            if (value.isEmpty) {
                              return "يرجى ادخال رقم الهاتف";
                            } else {
                              return null;
                            }
                          },
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(
                          top: MainModel().setPadding(
                              MainModel().middlePadding, widthScreen)),
                    ),
                    Directionality(
                      textDirection: TextDirection.rtl,
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 20),
                        margin: EdgeInsets.symmetric(horizontal: 20),
                        alignment: Alignment.centerRight,
                        decoration: boxDecoration(),
                        child: TextFormField(
                          controller: _emType,
                          textDirection: TextDirection.rtl,
                          autofocus: true,
                          decoration: InputDecoration(
                              border: InputBorder.none,
                              hintStyle: textStyle(),
                              hintText: 'نوع الابلاغ'),
                          validator: (value) {
                            if (value.isEmpty) {
                              return "يرجى ادخال نوع الابلاغ (الحالة)";
                            } else {
                              return null;
                            }
                          },
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(
                          top: MainModel().setPadding(
                              MainModel().middlePadding, widthScreen)),
                    ),
                    Container(
                      child: GestureDetector(
                          onTap: () async {
                            bool isLocationServiceEnabled =
                                await Geolocator.isLocationServiceEnabled();
                            if (isLocationServiceEnabled == true) {
                              Position position =
                                  await Geolocator.getCurrentPosition(
                                      desiredAccuracy: LocationAccuracy.high);
                              lon = position.longitude;
                              lat = position.latitude;
                            } else {
                              print(
                                  'امنح التطبيق من الوصول الى موقع... ثم اعد الارسال');
                            }
                          },
                          child: Image.asset('images/location.png')),
                    ),
                    Padding(
                        padding: EdgeInsets.only(
                            bottom: MainModel().setPadding(
                                MainModel().largePadding * 2, widthScreen))),
                    RaisedButton(
                      onPressed: () {
                        Firebase.initializeApp();
                        if (formKey.currentState.validate()) {
                          // save data in firebase

                          FirebaseFirestore.instance
                              .collection('Data')
                              .doc()
                              .set({
                            'name': _name,
                            'phone': _phone,
                            'em-type': _emType,
                          });

                          scaffoldKey.currentState.showSnackBar(SnackBar(
                              content: Text('تم ارسال بياناتك .... '
                                  'سيتم التواصل معك من الجهات المختصة')));
                        }
                      },
                      child: Text('ارسال'),
                    )
                  ],
                ))
          ],
        ));   }

  Widget notConnection() {
    final widthScreen = MediaQuery.of(context).size.width;
    return Scaffold(
        key: scaffoldKey,
        backgroundColor: MainModel().mainColor,
        appBar: AppBar(
          backgroundColor: Color(0xff323266),
          leading: IconButton(
              icon: Icon(
                Icons.notifications_active,
                color: MainModel().thirdColor,
              ),
              onPressed: () => {}),
          title: Logo(),
          centerTitle: true,
          actions: [
            Builder(
              builder: (context) => IconButton(
                icon: Icon(
                  Icons.menu,
                  color: MainModel().thirdColor,
                ),
                onPressed: () => Scaffold.of(context).openEndDrawer(),
                tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
              ),
            ),
          ],
        ),
        endDrawer: Drawer(
          child: MyDrawer(),
        ),
        body: ListView(
            padding: EdgeInsets.only(
                top: MainModel()
                    .setPadding(MainModel().largePadding, widthScreen)),
            children: [
              Container(
                child: Center(
                  child: Text('not Connection of Database'),
                ),
              )
            ]));   }

  Widget Loading() {
    final widthScreen = MediaQuery.of(context).size.width;
    return Scaffold(
        key: scaffoldKey,
        backgroundColor: MainModel().mainColor,
        appBar: AppBar(
          backgroundColor: Color(0xff323266),
          leading: IconButton(
              icon: Icon(
                Icons.notifications_active,
                color: MainModel().thirdColor,
              ),
              onPressed: () => {}),
          title: Logo(),
          centerTitle: true,
          actions: [
            Builder(
              builder: (context) => IconButton(
                icon: Icon(
                  Icons.menu,
                  color: MainModel().thirdColor,
                ),
                onPressed: () => Scaffold.of(context).openEndDrawer(),
                tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
              ),
            ),
          ],
        ),
        endDrawer: Drawer(
          child: MyDrawer(),
        ),
        body: Container(
    child: Center(
        child: Text('Loading'),
        ),
    )
    );   } }

我在编译器上没有任何错误,但没有连接 firebase。只是我想从 textfeild 获取数据并保存在 firebase 中。我的数据是(姓名、电话、位置、紧急情况类型)。

标签: firebaseflutterdartgoogle-cloud-firestore

解决方案


推荐阅读