Home > Software design >  The named parameter child is required but there's no corresponding argument
The named parameter child is required but there's no corresponding argument

Time:02-09

After upgrade my Flutter app is now producing this error at

return BaseWidget<BillsModel>(

The named parameter child is required but there's no corresponding argument.

My BaseWidget has a child parameter but I don't know how to specify the child. This code previously worked but now doesn't. I realise there are many similar questions but they are sufficiently different that I can't figure this out. I have many similar errors in my project which all extend from BaseWidget

class Bills extends StatelessWidget {

  const Bills({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    Tbl _table = Provider.of<Tbl>(context, listen: false);

    return BaseWidget<BillsModel>(
        
        model: BillsModel(api: Provider.of(context, listen: false)),
            
        onModelReady: (model) => model.fetchBills(context, _table.id),

        builder: (context, model, child) => model.busy

            ? Center(

                child: CircularProgressIndicator(),
              
              )
            
            : Expanded(
          
                child: ListView.builder(

                          shrinkWrap: true,
    
                          itemCount: model.bills.length,
    
                          itemBuilder: (context, index) => BillListItem(
    
                               bill: model.bills[index],   
                          ),
                )
            )
    );
  }
}

Here is my BillsModel

class BillsModel extends BaseModel {
  
  Api _api;

  BillsModel({required Api api}) : _api = api;

  List<Bill> bills = [];

  Future fetchBills(BuildContext context, int tableId) async {
    setBusy(true);
    bills = await _api.getBills(context, tableId);
    setBusy(false);
  }

 ...

  @override
  void dispose() {
    print('Bills has been disposed!!');
    super.dispose();
  }
}

Here is my BaseWidget:

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:

You should pass child parameters with any widget as your BaseWidget according to BaseWidget class.

Add an example code line, check it please

class Bills extends StatelessWidget {

  const Bills({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    Tbl _table = Provider.of<Tbl>(context, listen: false);

    return BaseWidget<BillsModel>(
        
        model: BillsModel(api: Provider.of(context, listen: false)),
            
        onModelReady: (model) => model.fetchBills(context, _table.id),
        child: const Sizedbox.shrink(), // Added This Line !
        builder: (context, model, child) => model.busy

            ? Center(

                child: CircularProgressIndicator(),
              
              )
            
            : Expanded(
          
                child: ListView.builder(

                          shrinkWrap: true,
    
                          itemCount: model.bills.length,
    
                          itemBuilder: (context, index) => BillListItem(
    
                               bill: model.bills[index],   
                          ),
                )
            )
    );
  }
}
  •  Tags:  
  • Related