Search code examples
flutterdartprovider

Can I use Future Provider in MultiProvider? But it can resolve both of them from the API


I am trying to use flutter Provider package and I have an API where I want to fetch data for Transport Type and the title of the route (it's the Marshes class). I decided to use Future Provider ans as I understand if I want to use mu;tiproviders I will need to use MultiProvider in my main fail on the top of the widhet tree. I did the same as it is described in docs but now I have an error: "The following ProviderNotFoundException was thrown building Home(dirty): Error: Could not find the correct Provider<List> above this Home Widget"

So the question is: Can I use the futureproviders in Multiproviders? As i did in a code below.

I didn't actually understand where is my mistake

the main file:

Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        FutureProvider(
          create: (context) => transportService.fetchTranspot(),
          initialData: [],
          catchError: (context, error) {
            print(error.toString());
          },
        ),
        FutureProvider(
          create: (context) => transportService.fetchMarshes(),
          catchError: (context, error) {
            print(error.toString());
          },
          initialData: [],
        ),
      ],
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Home(),
      ),

the other one for Transport Type:

List<TransportType> transport = Provider.of<List<TransportType>>(context);
    return Scaffold(
      appBar: AppBar(title: Text('Employees'),
      ),
      body: (transport == null)
      ? Center(child: CircularProgressIndicator(),)
      : ListView.builder(
        itemCount: transport.length,
        itemBuilder: (context,index){
          return ListTile(
            title: Text(transport[index].ttTitle),

also the same for the Marshes. I want to make it as a List

 final int ttId;
  final TransportService transportService = TransportService();

  EmployeePage({@required this.ttId});

  @override
  Widget build(BuildContext context) {
    List<Marshes> marsh = Provider.of<List<Marshes>>(context);
    return Scaffold(
        appBar: AppBar(title: Text('Employees'),
        ),
        body: (marsh == null)
            ? Center(child: CircularProgressIndicator(),)
            : ListView.builder(
            itemCount: marsh.length,
            itemBuilder: (context,index){
              return ListTile(
                title: Text(marsh[index].mrTitle),

Solution

  • You are not specifying the type of your FutureBuilders, for example:

    providers: [
        FutureProvider(
    

    should be:

    providers: [
        FutureProvider<List<TransportType>>(