我正在使用Flutter提供程序,但是我遇到了这个问题,我进行了很多搜索,但找不到解决方案,也许我导入软件包的方式错误,更改了它,但仍然是相同的错误
这是我的提供商代码(AuthService)
import 'package:firebase_auth/firebase_auth.dart';
import 'dart:async';
import 'package:flutter/material.dart';
class AuthService with ChangeNotifier {
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<FirebaseUser> getUser() {
return _auth.currentUser();
}
Future logout() async {
var result = FirebaseAuth.instance.signOut();
notifyListeners();
return result;
}
Future<FirebaseUser> loginUser({String email, String password}) async {
try {
var result = await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);
notifyListeners();
return result.user;
} catch (e) {
throw new AuthException(e.code, e.message);
}
}
Future<FirebaseUser> signupUser({String email,String password}) async{
try{
var result = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email,password: password);
notifyListeners();
return result.user;
}catch(e){
throw new AuthException(e.code, e.message);
}
}
}
这是我的主要爱好,我在这里使用MultiProvider
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import './providers/auth_service.dart';
import './screens/authenticate_screen.dart';
import './screens/edit_categories_screen.dart';
import './providers/categories.dart';
import './screens/category_screen.dart';
import './screens/categories_overview_screen.dart';
import './screens/add_item_screen.dart';
void main(List<String> args) {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<Categories>(
create: (_) => Categories(),
),
Provider<AuthService>(
create: (_)=>AuthService(),
)
],
child: MaterialApp(
title: 'Save All Me',
theme: ThemeData(primarySwatch: Colors.purple),
home: FutureBuilder<FirebaseUser>(
future: Provider.of<AuthService>(context).getUser(),
builder: (context, snapshot){
if(snapshot.connectionState==ConnectionState.done){
if(snapshot.error!=null){
print('error');
return Text(snapshot.error.toString());
}
return snapshot.hasData?CategoriesOverviewScreen(snapshot.data):AuthScreen();
}else{
return Center(child: CircularProgressIndicator());
}
},
),
routes: {
AddItemScreen.routeName: (context) => AddItemScreen(),
CategoryScreen.routeName:(context)=>CategoryScreen(),
EditCategoriesScreen.routeName:(context)=>EditCategoriesScreen(),
},
),
);
}
}
and this is the error 任何帮助,请^^