Home > database >  How to assign a callback function to a variable
How to assign a callback function to a variable

Time:03-15

I got this list with some items and each item got a function(MaterialPageRoute). When I print this list in my Screen and wrap it inside a material button ,I want to use the variable onTapAction .

import 'package:admin/constants.dart';
import 'package:flutter/material.dart';

class Raccourcis {
  final String imgSrc, title, onTapAction;

  Raccourcis({this.imgSrc, this.title, this.onTapAction, info});
}

List listeRaccourcis = [
  Raccourcis(
      title: "Ajouter Un Bon de commande",
      imgSrc: "assets/images/commande.png",
      onTapAction:
          "Navigator.push(context,MaterialPageRoute(builder: (context) => const SecondRoute()),);"),

This is how I'm using the list in another Widget :

InkWell(
    onTap: info.onTapAction,
 )

I'm getting the error that : The argument type 'String' can't be assigned to the parameter type 'void Function()'

CodePudding user response:

Your onTapAction parameter in Raccouris class is of type String, it should be of type Function.

class Raccourcis {
  final String imgSrc, title;
  final Function onTapAction;

  Raccourcis({
    required this.imgSrc,
    required this.title,
    required this.onTapAction,
  });
}

class Test extends StatefulWidget {
  const Test({Key? key}) : super(key: key);

  @override
  State<Test> createState() => _TestState();
}

class _TestState extends State<Test> {
  List<Raccourcis> listeRaccourcis = [];
  @override
  void initState() {
    super.initState();
    listeRaccourcis = [
      Raccourcis(
        title: "Ajouter Un Bon de commande",
        imgSrc: "assets/images/commande.png",
        onTapAction: () => Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => ScreenB()),
        ),
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: listeRaccourcis
              .map(
                (e) => GestureDetector(
                  onTap: () => e.onTapAction(),
                  child: Text(e.title),
                ),
              )
              .toList(),
        ),
      ),
    );
  }
}

CodePudding user response:

You have put some quotes, so it's a string, and you can't assign a string to a void Function parameter, as the error says.

You shoud do instead:

onTapAction:
      Navigator.push(context,MaterialPageRoute(builder: (context) => const SecondRoute()),);)

CodePudding user response:

The problem is that you are declaring the onTapAction as a String and not as a void() Func. Your onTapAction should look like this

onTapAction: {
         Navigator.push(context,MaterialPageRoute(builder: (context) => const SecondRoute()));
      }

or this

onTapAction: () =>
         Navigator.push(context,MaterialPageRoute(builder: (context) => const SecondRoute()))

Also, you should change your Raccourcis class to:

class Raccourcis {
      final String imgSrc, title;
      final Function() onTapAction;
    
      Raccourcis({this.imgSrc, this.title, required this.onTapAction, info});
}

CodePudding user response:

I would recommend using the VoidCallback and ValueChanged types. They are used in all Flutter UI widgets.

VoidCallback: https://api.flutter.dev/flutter/dart-ui/VoidCallback.html

ValueChanged: https://api.flutter.dev/flutter/foundation/ValueChanged.html

  • Related