flutterdarttypeerror

Flutter Provider TypeError: 'Null' is not a subtype of type 'String'


Issue: I'm using provider 6.1.2 in my Flutter project, and I'm encountering this error:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════

The following _TypeError was thrown building _InheritedProviderScope<FeedProvider?>(value: ): type 'Null' is not a subtype of type 'String'

The relevant error-causing widget is _InheritedProviderScope<FeedProvider?>.

    @override
_InheritedProviderElement<T> createElement() {
  return _InheritedProviderElement<T>(this);
}

@override
Widget buildWithChild(BuildContext context, Widget? child) {
  assert(
    builder != null || child != null,
    '$runtimeType used outside of MultiProvider must specify a child',
  );
  return _InheritedProviderScope<T?>(
    owner: this,
    // ignore: no_runtimetype_tostring
    debugType: kDebugMode ? '${runtimeType.toString()}' : '',
    child: builder != null
        ? Builder(
            builder: (context) => builder!(context, child),
          )
        : child ?? Container(),
  );
}

Solution

  • I believe the error might be coming from this line:
    debugType: kDebugMode ? '${runtimeType.toString()}' : '',.

    You could try simplifying it to:
    debugType: kDebugMode ? runtimeType.toString() : '',
    or
    debugType: kDebugMode ? '${runtimeType.toString() ?? ''}' : '',.

    Let me know if this helps! 😊