如何从Firestore文档Flutter获取文档ID

时间:2020-08-15 16:23:14

标签: firebase flutter firebase-authentication document

这是我的代码,经过数天的努力,我基本上想在不知道具体文档ID的情况下获取特定文档的ID。当我将文档ID复制并粘贴到代码中时,它可以工作并且我的数据会更新,但是当我不知道文档ID时该怎么办。请帮忙!编码新手:)

我已经尝试了各种各样的方法,但是firebase文档令人困惑,并且遗憾地没有包含Flutter文档。

       enum DegreeType { undergraduate, undergraduateintegrated, masters }
DocumentReference docRef =  Firestore.instance.collection('User').document();



class FirestoreID {
 String myId = docRef.documentID;
  FirestoreID({this.myId});
}



class SelectDegreeTypeScreen extends StatefulWidget {
  static const String id = 'selectdegreetypescreen';
  @override
  _SelectDegreeTypeScreenState createState() => _SelectDegreeTypeScreenState();
}

class _SelectDegreeTypeScreenState extends State<SelectDegreeTypeScreen> {

  DegreeType degreeType = DegreeType.undergraduate;
  final _auth = FirebaseAuth.instance;
  final _firestore = Firestore.instance;
  FirebaseUser loggedInUser;
  FirestoreID id;




  @override
  void initState() {
    super.initState();
    getCurrentUser();
  }

  void getCurrentUser() async {
    try {
      final user = await _auth.currentUser();
      if (user != null) {
        loggedInUser = user;
        String uid = user.uid;
        print(loggedInUser.email);
        print(loggedInUser.uid);
      }
    } catch (e) {
      print(e);
    }
  }




  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Padding(
        padding: EdgeInsets.only(left: 20.0, right:20, bottom: 50),
        child: Column(
          children: <Widget>[
            TopBarBackArrow(),
            LinearPercentIndicator(
              width: MediaQuery.of(context).size.width - 50,
              animation: true,
              lineHeight: 15.0,
              animationDuration: 1000,
              percent: 0.16,
              linearStrokeCap: LinearStrokeCap.roundAll,
              progressColor: Color(0xFF77a3cc),
            ),
            Padding(
              padding: EdgeInsets.only(top:20.0),
              child: Text(
                'Select your degree type.',
                style: kAppText,
              ),
            ),
            Expanded(
              child:ListView(
                physics: NeverScrollableScrollPhysics(),
                children: <Widget>[
                  ListTile(
                    title: const Text('Undergraduate', style: kAppTextsmallest,),
                    leading: Radio(
                      activeColor: Color(0xFF77a3cc),
                      value: DegreeType.undergraduate,
                      groupValue: degreeType,
                      onChanged: (DegreeType value)  {
                        setState(()  {
                          degreeType = value;
                         _firestore.collection('User').document(id.myId).updateData({'degree_type': 'undergrad'});
                        });
                      },
                    ),
                  ),
                  ListTile(
                    title: const Text('Undergraduate with Integrated Masters',  style: kAppTextsmallest),
                    leading: Radio(
                      activeColor: Color(0xFF77a3cc),
                      value: DegreeType.undergraduateintegrated,
                      groupValue: degreeType,
                      onChanged: (DegreeType value) {
                        setState(() {
                          degreeType = value;
                         _firestore.collection('User').document(id.myId).updateData({'degree_type': 'integrated'});
                        });
                      },
                    ),
                  ),
                  ListTile(
                    title: const Text('Masters',  style: kAppTextsmallest),
                    leading: Radio(
                      activeColor: Color(0xFF77a3cc),
                      value: DegreeType.masters,
                      groupValue: degreeType,
                      onChanged: (DegreeType value) {
                        setState(() {
                          degreeType = value;
                        _firestore.collection('User').document(id.myId).updateData({'degree_type': 'masters'});
                        });
                      },
                    ),
                  ),
                ],
              ),
            ),
            ReusableButton(
              title: 'Next',
              colour: Color(0xff77a3cc),
              onPressed:() {
                  Navigator.pushNamed(context, SelectedDegreeYears.id);
              },
            ),
          ],
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

如果您不知道文档的ID,则必须使用其他有关其字段的知识来查询它。这是从Firestore获取信息的仅有两种方法-按ID get(),或按字段内容查询(或查询整个集合以获取所有内容)。

我建议阅读documentation来了解查询。 flutter API与其他平台非常相似,使用Flutter的Firestore的API文档为here