我在Flutter代码main.dart文件中有一些错误。我创建了AuthService类,在main.dart中创建了Provider方法,但在调用Provider方法时有一些错误。我需要解决这个错误。在此处输入图像描述
import 'package:flutter/material.dart';
import 'package:trip/screen/first_screen.dart';
import 'package:trip/screen/sign_up_screen.dart';
import 'package:trip/services/auth_service.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Provider(
auth: AuthService,
child: MaterialApp(
title: 'Test FireBase',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: FirstScreen(),
routes: <String, WidgetBuilder>{
'/signUp': (BuildContext context) => SignUpScreen(),
'/home': (BuildContext context) => FirstScreen(),
},
),
);
}
}
class Provider extends InheritedWidget {
final AuthService auth;
Provider(Key key, Widget child, this.auth) : super(key: key, child: child);
@override
bool updateShouldNotify(InheritedWidget oldWidget) => true;
static Provider of(BuildContext context) =>
(context.dependOnInheritedWidgetOfExactType<Provider>() as Provider);
}
您得到错误是因为您创建的Provider
构造函数使用的是位置参数,而不是命名参数。有关命名参数和位置参数的详细信息,请查看本文。
您可以更改构造函数以使用以下命名参数:
Provider({Key? key, required Widget child, required this.auth}) : super(key: key, child: child);
或者你可以删除你的参数在协导器调用中的命名,如下所示:
return Provider(
UniqueKey(),
MaterialApp(
...
),
AuthService(),
);
我更喜欢第一种解决方案。