skip to Main Content

I cannot use function that has optional parameter in child widget(name is FixUI).This is original function declared in parent widgetThis is the line error occured(red line: Underfined~)

I want to make fixContact function arguments : fixContact, index, contact but in optional. How can I do it? Does flutter has this feature? Are there any other ways? Or did I missunderstood anything?

4

Answers


  1. Chosen as BEST ANSWER

    I found a stupid mistake... I confused constructor and function parameter.


  2. Make this changes :

    class FixUI extends StatefulWidget {
      final Function? fixContact;
      final int? index;
      List? contact;
    
      const FixUI({
        super.key,
        this.fixContact,
        this.index,
        this.contact,
      });
    
      @override
      State<FixUI> createState() => _FixUIState();
    }
    
    Login or Signup to reply.
  3. To make widget arguments optional, you either have to make them nullable or provide a default value. Also, currently, you do not specify Function arguments, but widget arguments. Function arguments in this case are optional if they are named – inside curly braces.

    Here is an example how to provide more information to your function.

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MainApp());
    }
    
    String bar({int? index, List<String> contact = const []}) {
      return 'foo';
    }
    
    class MainApp extends StatelessWidget {
      const MainApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          home: Scaffold(
            body: Center(
              child: Foo(
                fixContact: bar,
              ),
            ),
          ),
        );
      }
    }
    
    class Foo extends StatefulWidget {
      const Foo({
        required this.fixContact, 
        super.key,
      });
    
      final String Function(
          {int index,
          List<String> contact
          }) fixContact;
    
      @override
      State<Foo> createState() => _FooState();
    }
    
    class _FooState extends State<Foo> {
      @override
      Widget build(BuildContext context) {
        return Text(widget.fixContact(index: 5));
      }
    }
    
    
    Login or Signup to reply.
    1. Define the callback function in the parent widget with the desired optional parameters. For example:typedef MyCallback = void Function(int optionalParam);
    2. In the parent widget, create an instance of the callback function and pass it to the child widget as a parameter. Make sure to provide the optional parameter if needed. For example:
        class ParentWidget extends StatelessWidget {
      final MyCallback callback;
    
      ParentWidget({this.callback});
    
      @override
      Widget build(BuildContext context) {
        return ChildWidget(callback: callback);
      }
    }
    

    3.In the child widget, define a callback property of the same type as the function signature.

    class ChildWidget extends StatelessWidget {
    final MyCallback callback;

    ChildWidget({this.callback});

    @override Widget build(BuildContext context) {
    // Use the callback function whenever needed
    return RaisedButton(
    onPressed: () {
    // You can call the callback with or without the optional parameter
    callback(42); // Optional parameter provided
    },
    child: Text(‘Call Parent Callback’),
    ); } }

    1. When using the parent and child widgets, pass the callback function to the parent widget and handle it accordingly. For example:

    ParentWidget( callback: (int optionalParam) {
    // Handle the callback function in the parent widget
    print(‘Optional parameter received: $optionalParam’); }, )

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search