1

From: https://medium.com/flutterdevs/working-with-callback-in-flutter-89dc207cba37

In the following code, I can see the declaration of the callback StringValue. I can see it being called.

I can't find out where its body is. That means when we write a function which receives some arguments, we do something with the arguments and then return something.

Where is all that happening in the callback here?

import 'package:flutter/material.dart';

typedef StringValue = String Function(String);

// ignore: must_be_immutable
class EditingPage extends StatefulWidget {
  _EditingPageState createState() => _EditingPageState();
  StringValue callback;
  EditingPage(this.callback);
}
class _EditingPageState extends State<EditingPage> {
  TextEditingController textController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Widget Communication"),
        backgroundColor: Colors.red,
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            height: 50,
            margin: EdgeInsets.all(8),
            child: TextFormField(
              onTap: () {},
              controller: textController,
              maxLines: 1,
              maxLength: 20,
              decoration: InputDecoration(
                hintText: "Enter Some Text Here",
                contentPadding: EdgeInsets.all(8),
              ),
            ),
          ),
          RaisedButton(
              color: Colors.red,
              child: Text("Submit"),
              onPressed: () {
                widget.callback(textController.text);
                Navigator.pop(context);
              }),
        ],
      ),
    );
  }
}
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • Does this answer your question? [What are function typedefs / function-type aliases in Dart?](https://stackoverflow.com/questions/12545762/what-are-function-typedefs-function-type-aliases-in-dart) – enzo May 17 '21 at 15:49
  • Check the place where `EditingPage` is used (Initialised). – Midhun MP May 17 '21 at 15:53
  • Here. https://imgur.com/a/UzjIsEh. A picture from the article. – Nisanth Reddy May 17 '21 at 16:29

1 Answers1

2

Is it the use of typedef that is not clear to you? It declares no functions but defines aliases. In fact, with the next version of the Dart Sdk it will be possible to define aliases also for types other than functions, for example typedef MyStringList = List<String>.

In the code you reported, callback is a field able to store function (to be precise, function with a String parameter and returning a String), which is setted with a constructor argument.

So when you instantiate EditingPage you will have to pass it a function of type String Function(String) (alias StringValue), for example:

var myCallback = (String value){
  return saveInMyDatabaseAndReturnId(value);
};
runApp(EditingPage(myCallback));
Mabsten
  • 1,570
  • 11
  • 16