-2

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?

MIN S
  • 21
  • 1

4 Answers4

1

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

MIN S
  • 21
  • 1
0

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();
}
Karan Mehta
  • 1,442
  • 13
  • 32
0

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));
  }
}

Wiktor
  • 726
  • 5
  • 20
0
  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'); }, )

PradeepKumar
  • 138
  • 8