仅在热重新加载后才启动Flutter Firestore数据

时间:2020-09-03 16:24:46

标签: firebase flutter dart google-cloud-firestore

我用...拍了一张表格

Stream tenthDetails;
  getdetails() async {
    var user = await FirebaseAuth.instance.currentUser();

    tenthDetails = DatabaseService(uid: user.uid).tenthDetails;
  }

  @override
  void initState() {
    getdetails();
    super.initState();
  }
@override
  Widget build(BuildContext context) {
    final user = Provider.of<User>(context);

    var width = MediaQuery.of(context).size.width;
    Size size = MediaQuery.of(context).size;

    changeStatusColor(food_white);
    var bottomButtons = Container(
      height: 50,
      decoration: BoxDecoration(boxShadow: [
        BoxShadow(
            color: Colors.grey.withOpacity(0.7),
            blurRadius: 16,
            spreadRadius: 2,
            offset: Offset(3, 1))
      ], color: food_white),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Container(
              child: text('Save',
                  textColor: food_white,
                  fontSize: textSizeLargeMedium,
                  fontFamily: fontMedium),
              color: food_colorPrimary,
              alignment: Alignment.center,
              height: double.infinity,
            ),
          )
        ],
      ),
    ).onTap(() async {
      if (_formKey.currentState.validate())
        await saveData(school ?? sc, board ?? bd, year ?? yr, percent ?? pr);
    });

    return StreamBuilder<Tenth>(
        stream: tenthDetails,
        builder: (BuildContext context, AsyncSnapshot<Tenth> snapshot) {
          print(snapshot.hasData);

          if (snapshot.hasData) {
            Tenth tenth = snapshot.data;

            sc = tenth.school;
            bd = tenth.board;
            yr = tenth.year;
            pr = tenth.percent;
            print(sc);

            return Scaffold(
                backgroundColor: food_app_background,
                body: SafeArea(
                  child: Form(
                    key: _formKey,
                    autovalidate: _autovalidate,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        mToolbar(
                          context,
                          'Class 10th Details',
                        ),
//              SizedBox(height: spacing_standard_new),
                        Expanded(
                          child: SingleChildScrollView(
                            child: Container(
                              padding: EdgeInsets.only(
                                  left: 16, bottom: 70, right: 16, top: 0),
//                    margin: EdgeInsets.all(spacing_standard_new),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.start,
                                mainAxisSize: MainAxisSize.min,
                                children: <Widget>[
                                  SizedBox(
                                    height: 24,
                                  ),
                                  Text(
                                    "Fill your information",
                                    style: TextStyle(
                                        fontFamily: fontBold,
                                        letterSpacing: 0.2),
                                  ),
                                  SizedBox(
                                    height: 24,
                                  ),
                                  basictextField(
                                      initialValue: tenth.school,
                                      labelText: 'School',
                                      image: Icons.person_outline,
                                      Validate: (value) {
                                        if (value.length <= 2) {
                                          return 'Please enter a valid School Name';
                                        }
                                        return null;
                                      },
                                      onChanged: (value) {
                                        setState(() {
                                          school = value;
                                        });
                                      }),
                                  SizedBox(
                                    height: 16,
                                  ),
                                  basictextField(
                                      labelText: 'Board',
                                      initialValue: bd,
                                      Validate: (value) {
                                        if (value.length <= 2) {
                                          return 'Please enter a valid School Board';
                                        }
                                        return null;
                                      },
                                      onChanged: (value) {
                                        setState(() {
                                          board = value;
                                        });
                                      }),
                                  SizedBox(
                                    height: 16,
                                  ),
                                  basictextField(
                                      initialValue: yr,
                                      labelText: 'Passed Year',
                                      image: Icons.person_outline,
                                      Validate: (value) {
                                        if (value.length <= 2) {
                                          return 'Please enter a valid passing Year';
                                        }
                                        return null;
                                      },
                                      onChanged: (value) {
                                        setState(() {
                                          year = value;
                                        });
                                      }),
                                  SizedBox(
                                    height: 16,
                                  ),
                                  basictextField(
                                      initialValue: pr,
                                      labelText: 'Percentage',
                                      textInputType: TextInputType.number,
                                      image: Icons.person_outline,
                                      Validate: (value) {
                                        if (value.length > 2) {
                                          return 'Please enter a valid percentage';
                                        }
                                        return null;
                                      },
                                      onChanged: (value) {
                                        setState(() {
                                          percent = value;
                                        });
                                      }),
                                ],
                              ),
                            ),
                          ),
                        ),
                        SizedBox(height: spacing_standard),
                        bottomButtons,
                      ],
                    ),
                  ),
                ));
          } else {
            print('abcdeddsdsdsdsds');
            return Text('loading');
           
        });
  }
}

带有一些自定义小部件

模型

class Tenth {
  final school;
  final board;
  final year;
  final percent;
  Tenth({this.school, this.board, this.year, this.percent});
}

数据库服务部分

 Firestore.instance.collection('StudentID');
  final CollectionReference interviewCollection =
      Firestore.instance.collection('InterviewSchedule');
Tenth _tenthListFromSnapshot(DocumentSnapshot snapshot) {
    print(snapshot.data);
    return Tenth(
      school: snapshot.data['tenthschool'] ?? '',
      board: snapshot.data['tenthboard'] ?? '',
      year: snapshot.data['tenthend_date'] ?? '',
      percent: snapshot.data['tenthscore'] ?? '',
    );
  }
Stream<Tenth> get tenthDetails {
    return userData
        .document(uid)
        .collection('EducationDetails')
        .document(uid)
        .snapshots()
        .map(_tenthListFromSnapshot);
  }

表单字段具有初始值设置,因此,当用户打开表单时,应加载firestore中的初始值,但是当打开表单屏幕时,它将停留在“加载”部分,并且仅在热后加载数据重新加载。一旦启动,我如何加载数据?

0 个答案:

没有答案