1

I have some error in the Flutter code main.dart file .I create AuthService class,I create Provider method in the main.dart file, but it has some error at the call up the Provider method . i need solve this error. enter image description here

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);
}

waw666
  • 26
  • 2

1 Answers1

0

You are getting the error because the Provider constructor you made is working with positional parameters, not with named. For more information on named and positional parameters check out this post.

You can either change your constructor to work with named parameters like this:

Provider({Key? key, required Widget child, required this.auth}) : super(key: key, child: child);

or you can remove namings of your parameters in the cosntructor call like this:

return Provider(
  UniqueKey(),
  MaterialApp(
    ...
  ),
  AuthService(),
);

I would prefer the first solution.

kforjan
  • 584
  • 5
  • 15