How to solve ‘Tried to use Provider with a subtype of Listenable-Stream’ Provider error in Flutter

How to solve ‘Tried to use Provider with a subtype of Listenable/Stream’ Provider error in Flutter

Provider is one of the most popular package of Flutter and you may come across the error “Tried to use Provider with a subtype of Listenable/Stream” while using in an application.

The Provider package may also recommend you a solution like this –

This is likely a mistake, as Provider will not automatically update dependents
when DataProvider is updated. Instead, consider changing Provider for more specific
implementation that handles the update mechanism, such as:

– ListenableProvider
– ChangeNotifierProvider
– ValueListenableProvider
– StreamProvider

There are couple of ways to solve this error depending on how you want to use the package.

Solution 1:

This is according to the message suggested from the Provider package.

If you want to use the Provider to access some data and also wants to update the state of an widget whenever the state changes  then instead of using Provider in the main.dart file (usually the provider is injected/provided here) you can use any of the suggested provider such as ListenableProvider.

example:

in main.dart file

void main() {
  runApp(
      ListenableProvider(create: (context) => DataProvider(),child: MyApp(), )
  );
}

Instead of ListenableProvider, you can use any other suggested provider depending on your application requirement.

Solution 2:

If you want to use your provider only to access some data across the application which never changes throughout the application, then you can make your class (provider class) do not extend ChangeNotifier

example:

import 'package:flutter/material.dart';

class DataProvider {
  final String imgSource = 'https://example.com/assets/images';  
}

Now you can use Provider class inside the main.dart file to provide the provider.

Leave A Comment

Your email address will not be published. Required fields are marked *