Skip to content

Instantly share code, notes, and snippets.

@hans-min
Last active June 13, 2023 12:14
Show Gist options
  • Save hans-min/6b24cbfdf1478e43ce79eb888ed8dc3c to your computer and use it in GitHub Desktop.
Save hans-min/6b24cbfdf1478e43ce79eb888ed8dc3c to your computer and use it in GitHub Desktop.
onSubmitted: func vs () => func()

onSubmitted: func vs () => func()

Created with <3 with dartpad.dev.

import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text("Func vs () => func"),
),
body: const Center(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: SearchBar(),
),
),
),
);
}
}
class SearchBar extends StatelessWidget {
const SearchBar({super.key});
@override
Widget build(BuildContext context) {
return TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
),
//onSubmitted: onSubmit, // Option A works
onSubmitted: (value) => onSubmit // Option B doesn't
//onSubmitted: (value) => onSubmit(value), // Option C works
);
}
onSubmit(value) => print(value);
//try assign onSubmitted: onSubmitNoParam, and see the error
onSubmitNoParam() => print("Printed!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment