Home > Blockchain >  The argument type 'xxx' can't be assigned to the parameter type 'Widget? Functio
The argument type 'xxx' can't be assigned to the parameter type 'Widget? Functio

Time:05-29

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_scaffold/pages/home_screen.dart';
import 'package:flutter_scaffold/pages/incrementCounter/incrementCounter.dart';

var mainHandler = Handler(
   handlerFunc: (BuildContext context, Map<String, List<String>> params) {
    return HomeScreen();
  }
);

var incrementCounterHandler = Handler(
  handlerFunc: (BuildContext context, Map<String, List<String>> params) {
    return IncrementCounter(title: '计数');
  }
);

enter image description here

the Handler class:

class Handler {
  Handler({this.type = HandlerType.route, required this.handlerFunc});
  final HandlerType type;
  final HandlerFunc handlerFunc;
}
typedef Widget? HandlerFunc(
    BuildContext? context, Map<String, List<String>> parameters);

I know the argument_type_not_assignable error in a simple way as below

String f(String x) => x; String g(String y) => f(y);

and common fix as this link

enter image description here

CodePudding user response:

If your sure the types are same you can do -

return HomeScreen() as Widget? Function(BuildContext?, Map<String, List<String>>);

Edit: Change the Handler file as -

typedef Widget? Function(BuildContext?, Map<String, List<String>>) HandlerFunc(
BuildContext? context, Map<String, List<String>> parameters);

Hope it helps.

CodePudding user response:

var mainHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
  return HomeScreen();
});

var incrementCounterHandler = Handler(
    handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
  return IncrementCounter(title: '计数');
});

all right, BuildContext can be null cause this error,just add a ? between BuildContext and context。

  • Related