【发布时间】:2023-04-01 11:26:01
【问题描述】:
我是 Flutter 的新手,我正在尝试创建一个“评分”页面,用户可以在其中选择 1-5 星并提交。然后我想将它与当前用户 ID(无论谁登录)一起添加到 firestore。以下是我获取当前用户 ID 的方法:
class AuthService {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
bool isLoggedIn = false;
Stream<String> get onAuthStateChanged =>
_firebaseAuth.onAuthStateChanged.map((FirebaseUser user) => user?.uid);
// GET UID
Future<String> getCurrentUID() async {
return (await _firebaseAuth.currentUser()).uid;
}
然后我尝试将其整合到我的评分页面中:
import 'package:easy_tiger/constants/style.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:provider/provider.dart';
import 'package:easy_tiger/services/auth_service.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class RateUs extends StatefulWidget {
@override
_RateUsState createState() => _RateUsState();
}
class _RateUsState extends State<RateUs> {
@override
Widget build(BuildContext context) {
String _currentUser = currentUser(context);
double finalRating = 0;
return Scaffold(
appBar:
AppBar(
leading: GestureDetector(
onTap: () => Navigator.pop(context),
child: Icon(Icons.arrow_back)),
title: Text('Rate us'),),
backgroundColor: Colors.white,
body: Column(
children: [
FlutterRatingBar(
initialRating: 5,
fillColor: kPrimaryColor,
borderColor: kPrimaryColor.withAlpha(50),
onRatingUpdate: (double rating) {finalRating = rating ;},
),
Center(
child: RaisedButton(
child: Text('Submit'),
elevation: 8.0,
onPressed: () =>
Firestore.instance.collection('ratings').document(_currentUser).setData({
'uID' : _currentUser,
'Rating' : finalRating,
'Submitted at' : DateTime.now()
})),
),
],
),);
}
}
String currentUser (BuildContext context) {
final auth = Provider.of(context).auth;
String currentUser = auth.getCurrentUID();
return currentUser;
}
但是得到以下错误:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following ProviderNotFoundError was thrown building RateUs(dirty, state: _RateUsState#ad8a3):
Error: Could not find the correct Provider<dynamic> above this RateUs Widget
如何在 RateUs 小部件上方添加正确的提供程序?
【问题讨论】:
标签: firebase flutter dart google-cloud-firestore