Home > other >  The argument type 'Widget Function(BuildContext, T, Widget)' can't be assigned to the
The argument type 'Widget Function(BuildContext, T, Widget)' can't be assigned to the

Time:02-08

I have a base widget class which no longer works since I upgraded the provider version to 5.0.0.

At builder: widget.builder I am getting this error:

The argument type 'Widget Function(BuildContext, T, Widget)' can't be assigned to the parameter type 'Widget Function(BuildContext, T, Widget?)

How can I fix this?

Here is my code

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class BaseWidget<T extends ChangeNotifier> extends StatefulWidget {
  final Widget Function(BuildContext context, T model, Widget child) builder;
  final T model;
  final Widget child;
  final Function(T) onModelReady;

  BaseWidget({
    Key? key,
    required this.builder,
    required this.model,
    required this.child,
    required this.onModelReady,
  }) : super(key: key);

  _BaseWidgetState<T> createState() => _BaseWidgetState<T>();
}

class _BaseWidgetState<T extends ChangeNotifier> extends State<BaseWidget<T>> {
  late T model;

  @override
  void initState() {
    
    model = widget.model;

      widget.onModelReady(model);

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<T>(
      create: (context) => model,
      child: Consumer<T>(
        builder: widget.builder,
        child: widget.child,
      ),
    );
  }
}

CodePudding user response:

I pasted the code into Android studio and it looks like to need to change the following:

From
final Widget Function(BuildContext context, T model, Widget child) builder;
To
final Widget Function(BuildContext context, T model, Widget? child) builder;
  •  Tags:  
  • Related