Skip to content

Instantly share code, notes, and snippets.

@gothedistance
Created August 3, 2021 07:52
Show Gist options
  • Save gothedistance/b4cfff81fb8fb91d9d9d28f2af8277bb to your computer and use it in GitHub Desktop.
Save gothedistance/b4cfff81fb8fb91d9d9d28f2af8277bb to your computer and use it in GitHub Desktop.
How to supply TextField initial value on flutter via async load.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
class SamplePage extends StatefulWidget {
@override
State<StatefulWidget> createState() => _SamplePageWidgetState();
}
class _SamplePageWidgetState extends State<LoginPage> {
final TextEditingController _email_controller = TextEditingController();
@override
void initState() {
super.initState();
}
void setLoginInfo() async {
FlutterSecureStorage st = new FlutterSecureStorage();
String? sample = await st.read(key: "something");
if(sample != null) {
setState(() {
_email_controller.text = sample;
});
}
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new SafeArea(child: _buildLoginPage(context)),
);
}
Widget _buildLoginPage(BuildContext context) {
return Center(
child: Column(
children: <Widget>[
TextField(
inputType: TextInputType.emailAddress,
controller: _email_controller,
),
]
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment