How to remove Alert Dialogs in Flutter - flutter

I have create a Alert Dialog for OTP Verification after verifying OTP I close it and then I had created a another dialog which is for data processing... and then I close it.
Result:-
First OTP Dialog closed after OTP verification by calling Navigator.of(context).pop(); and then second dialog just pops up but It does not closed after calling Navigator.of(context).pop();
What I want to do:
Close OTP Dialog after verifying OTP (Works)
Open Progress dialog (Works)
Close it after uploading profile in firebase storage (Does not Works)
Please help me solve this issue.
Thanks in Advance !

You probably forgetting await somewhere in your code.
Try this,
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
TextEditingController _otpCtrl = TextEditingController();
void dispose() {
_otpCtrl.dispose();
super.dispose();
}
Future<void> _verifyOTP() async {
final String otp = await _inputOtp();
String otpValidationError;
if (otp != null) otpValidationError = await _sendOtpVerifyRequest();
print(otpValidationError);
}
Future<String> _sendOtpVerifyRequest() async {
showDialog(
context: context,
builder: (context) {
return Center(child: CircularProgressIndicator());
},
);
await Future.delayed(Duration(seconds: 2)); //TODO: Do post request here
Navigator.pop(context);
return null;
}
Future<String> _inputOtp() async {
final flag = await showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Enter OTP"),
content: TextField(
controller: _otpCtrl,
decoration: InputDecoration(
hintText: "x x x x x x",
),
),
actions: <Widget>[
FlatButton(
child: Text("Cancel"),
onPressed: () {
Navigator.pop(context, false);
},
),
FlatButton(
child: Text("Confirm"),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
},
);
if (flag == true)
return _otpCtrl.text;
else
return null;
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: _verifyOTP,
child: Text("Click Here"),
),
),
);
}
}

Related

Flutter use_build_context_synchronously and SearchDelegate.close after await

My use case: I show a search UI the user can pick an item with - when the user taps an item, something async happens and depending on the result, close (https://api.flutter.dev/flutter/material/SearchDelegate/close.html) is called with either the picked item or a null. In my snippet below the async something is simply asking the user for confirmation with an AlertDialog.
This works without an issue but I updated the lint rules recently, and turned this one on: https://dart-lang.github.io/linter/lints/use_build_context_synchronously.html. Now the linter complains about the BuildContext being used after an await in the call to close. Here is the snippet (full reproducible sample below, written with Flutter 2.10.4):
onTap: () async {
final confirmed = await _confirm(context, item) ?? false;
// Triggers 'Do not use BuildContexts across async gaps.'
// https://dart-lang.github.io/linter/lints/use_build_context_synchronously.html
close(context, confirmed ? item : null);
},
I guess this makes sense and could be dangerous in some scenarios, so I'd better fix it. My question is: how do I implement my use case the 'right' way? One solution that works is to extract suggestions to a separate StatefulWidget and guard the call with a isMounted check, but this has the following drawbacks:
It requires a separate StatefulWidget that I would otherwise not need (boilerplate I don't want).
I need to pass a callback to the widget to call close (it belongs to SearchDelegate but now it will be called by code in the widget).
As close requires a BuildContext, I either have to pass the one that the SearchDelegate has to the widget to use (yuck) or just use the one from my widget - in this case it works (as close just uses Navigator.of(context)), but what if it were necessary to pass exactly the one from SearchDelegate?
Full code:
import 'package:flutter/material.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: _Home(),
);
}
}
class _Home extends StatefulWidget {
#override
State<_Home> createState() => _HomeState();
}
class _HomeState extends State<_Home> {
String? _picked;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
TextButton(
onPressed: () => _maybePick(context),
child: const Text('Maybe pick'),
),
Center(
child: Text(_picked != null ? 'Last pick: $_picked' : 'No pick'),
),
],
),
);
}
Future<void> _maybePick(BuildContext context) async {
final result = await showSearch<String?>(
context: context,
delegate: _PickerDelegate(),
);
if (result != null) {
setState(() {
_picked = result;
});
}
}
}
class _PickerDelegate extends SearchDelegate<String?> {
final _allItems = List.generate(200, (index) => 'Item $index');
#override
Widget buildLeading(BuildContext context) {
return IconButton(
onPressed: () => close(context, null),
icon: const BackButtonIcon(),
);
}
#override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
onPressed: () => query = '',
icon: const Icon(Icons.clear),
),
];
}
#override
Widget buildSuggestions(BuildContext context) {
final items = _allItems.where((element) => element.contains(query));
return ListView(
children: items.map((item) {
return ListTile(
title: Text(item),
onTap: () async {
final confirmed = await _confirm(context, item) ?? false;
// Triggers 'Do not use BuildContexts across async gaps.'
// https://dart-lang.github.io/linter/lints/use_build_context_synchronously.html
close(context, confirmed ? item : null);
},
);
}).toList(growable: false),
);
}
#override
Widget buildResults(BuildContext context) {
// Keep it simple for the snippet.
throw UnimplementedError('results are not supported');
}
Future<bool?> _confirm(BuildContext context, String item) async {
return showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
content: Text("Pick '$item'?"),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: const Text('Yes'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text('No'),
),
],
);
},
);
}
}

Flutter - A TextEditingController was used after being disposed running tests

Still on the AlertDialog validation example I recently posted, I ran into another odd problem. I fixed the validation code as per the below and it works!
import "package:flutter/material.dart";
import "package:easy_wallet/resources/constants.dart";
class WalletApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(title: "EasyWallet", home: EasyWalletHomePage());
}
}
class EasyWalletHomePage extends StatefulWidget {
#override
_EasyWalletState createState() => _EasyWalletState();
}
class _EasyWalletState extends State<EasyWalletHomePage> {
final TextEditingController _nameController = TextEditingController();
#override
void initState() {
super.initState();
}
#override
void dispose() {
_nameController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
print("BUILD1!");
return Scaffold(
body: Center(
child: Wrap(
direction: Axis.vertical,
crossAxisAlignment: WrapCrossAlignment.center,
spacing: 15,
children: [
Text("No wallets yet..."),
ElevatedButton(
key: KEY_ADD_WALLET,
child: const Text("➕"),
style: ElevatedButton.styleFrom(
shape: CircleBorder(),
),
onPressed: () {
_showAddWalletDialog(context);
})
],
),
),
);
}
Future<void> _showAddWalletDialog(BuildContext context) async {
print("BUILD2!");
return await showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: const Text('Add wallet'),
content: Wrap(
children: [
Text("Insert wallet name:"),
TextField(
controller: _nameController,
maxLength: 40,
onChanged: (value) {
setState(() {});
},
decoration: InputDecoration(
hintText: "eg: home"
),
),
],
),
actions: <Widget>[
TextButton(
child: const Text('CANCEL'),
onPressed: () {
setState(() {
Navigator.pop(context);
});
},
),
TextButton(
child: const Text('OK'),
onPressed: (!_isValidName()) ? null : () {
Navigator.pop(context);
},
)
],
);
}
);
});
}
bool _isValidName() {
return _nameController.value.text.isNotEmpty;
}
}
void main() {
runApp(WalletApp());
}
I want now to go back developing in TDD and started I wrote the test:
testWidgets('show add wallet dialog', (WidgetTester tester) async {
await tester.pumpWidget(WalletApp());
expect(find.byType(Dialog), findsNothing);
var button = find.byKey(KEY_ADD_WALLET);
await tester.tap(button);
await tester.pumpAndSettle();
expect(find.byType(Dialog), findsOneWidget);
});
however, when I run it, I get the error:
The following assertion was thrown while finalizing the widget tree: A
TextEditingController was used after being disposed. Once you have
called dispose() on a TextEditingController, it can no longer be used.
When the exception was thrown, this was the stack:
#0 ChangeNotifier._debugAssertNotDisposed. (package:flutter/src/foundation/change_notifier.dart:114:9)
#1 ChangeNotifier._debugAssertNotDisposed (package:flutter/src/foundation/change_notifier.dart:120:6)
following, a long stack trace.
If I remove _nameController.dispose(); the the test runs...
What am I doing wrong?
For the (few at this point :) ) people that use TDD with flutter, this appears to be a bug. It is working on master channel (see https://github.com/flutter/flutter/issues/98965). Thanks to flutter developers for looking into this issue.

stop closing showDialogue itself after 15 seconds

I am trying to display an information dialog when starting an application. After closing, another window appears asking for permission. I call it all in the initState function. It works, but I noticed that this first info dialog also closes on its own when 15 seconds have elapsed. How do I fix this? So that while the dialog is not closed by the user, the application will not be loaded further?
class _MyAppState extends State<MyApp> {
final keyIsFirstLoaded = 'is_first_loaded';
#override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
final context = MyApp.navKey.currentState.overlay.context;
await showDialogIfFirstLoaded(context);
await initPlatformState();
});
}
showDialogIfFirstLoaded(BuildContext context, prefs) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool isFirstLoaded = prefs.getBool(keyIsFirstLoaded);
if (isFirstLoaded == null) {
return showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return new AlertDialog(
// title: new Text("title"),
content: new Text("//"),
actions: <Widget>[
new FlatButton(
child: new Text(".."),
onPressed: () {
Navigator.of(context).pop();
prefs.setBool(keyIsFirstLoaded, false);
},
),
],
);
},
);
}
}
initPlatformState() async {
print('Initializing...');
await BackgroundLocator.initialize();
print('Initialization done');
final _isRunning = await BackgroundLocator.isRegisterLocationUpdate();
setState(() {
isRunning = _isRunning;
});
onStart();
print('Running ${isRunning.toString()}');
}
#override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
// ... app-specific localization delegate[s] here
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
navigatorKey:MyApp.navKey,
navigatorObservers: [
FirebaseAnalyticsObserver(analytics: analytics),
],
debugShowCheckedModeBanner: false,
title: '',
theme: ThemeData(),
home: new SplashScreen(),}
class SplashScreen extends StatefulWidget {
#override
_SplashScreenState createState() => new _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> with SingleTickerProviderStateMixin {
Timer _timer;
bool _visible = true;
startTime() async {
_timer = Timer(new Duration(seconds: 5), navigationPage);
}
void navigationPage() {
Navigator.of(context).pushReplacementNamed('/home');
}
#override
void initState() {
_timer = Timer(Duration(seconds: 4),
() => setState(
() {
_visible = !_visible;
},
),
);
startTime();
super.initState();
}
#override
void dispose() {
_timer.cancel();
super.dispose();
}
#override
Widget build(BuildContext context) {
return new Stack(
children: <Widget>[
Container(
width: double.infinity,
child: Image.asset('images/bg.jpg',
fit: BoxFit.cover,
height: 1200,
),
),
Container(
width: double.infinity,
height: 1200,
color: Color.fromRGBO(0, 0, 0, 0.8),
),
Container(
alignment: Alignment.center,
child: Row(
children: <Widget>[
Expanded(
flex: 2,
child: Container(
child: Text(''),
),
),
],
),
),
],
);
}
}
This code displays an Alert dialogue if the user is new and after the button click, it will direct him to another dialogue.
I have tested the code and it doesn't close after 15 seconds. I'm still not sure what you're trying to accomplish but I hope this helps.
Init State
#override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) async {
await dialog1(context);
//await initPlatformState();
});
super.initState();
}
Alert Dialog 1
dialog1(BuildContext context)async{
SharedPreferences prefs = await SharedPreferences.getInstance();
bool isFirstLoaded = prefs.getBool("keyIsFirstLoaded")??true;
if (isFirstLoaded) {
showDialog(
barrierDismissible: false, //disables user from dismissing the dialog by clicking out of the dialog
context: context, builder: (ctx) {
return AlertDialog(
title: Text("dialog 1"), content: Text("Content"), actions: [
TextButton(
child: new Text(".."),
onPressed: () async{
Navigator.pop(ctx);
await dialog2(context);
prefs.setBool("keyIsFirstLoaded", false);
},
),],);
},);
}else{
//not first time
}
}
Alert Dialog 2
void dialog2(BuildContext context)async{
print("dialog 2");
showDialog(context: context, builder: (context) {
return AlertDialog(title: Text("Dialog 2"),content: Text("permissions"),actions: [
TextButton(
child: new Text("close"),
onPressed: () async{
Navigator.pop(context);
//await dialog1(context); //uncomment if you want to go back to dialoge 1
},
),],);
},);
}
You can return a value from Navigator in the first dialog
Navigator.of(context).pop(true);
prefs.setBool(keyIsFirstLoaded, false);
Once it receive true, then only call the second method.
var value = await showDialogIfFirstLoaded(context);
if(value == true) {
await initPlatformState();
}

ShowDialog from an asynchronus function called in initState Flutter

I am writing a code to check if the user have the correct version of my app installed otherwise redirect him on the Playstore to update the app.
I am using firebase_remote_config to store a variable to keep tract of the minimum version I want the users to use.
Package_info to get information about users app.
url_launcher to redirect to playstore from the app
My problem is that my method to check the version is asynchronous and it needs to show the dialog to the user before entering the first screen of the app.
I execute it in initState. But the build method builds the first screen before the end of my asynchronous function and my ShowDialog did not render.
How can I firstly show the result of my asynchronous function before the first build?
here is my code after some updates but not showing the Dialog before Navigating to another screen
class Splashscreen extends StatefulWidget {
#override
_SplashscreenState createState() => _SplashscreenState();
}
class _SplashscreenState extends State<Splashscreen> {
#override
void initState() {
super.initState();
SchedulerBinding.instance.addPostFrameCallback((_) {
versionCheck(context);
});
}
Future versionCheck(context) async {
//Get Current installed version of app
final PackageInfo info = await PackageInfo.fromPlatform();
double currentVersion =
double.parse(info.version.trim().replaceAll(".", ""));
//Get Latest version info from firebase config
final RemoteConfig remoteConfig = await RemoteConfig.instance;
try {
// Using default duration to force fetching from remote server.
await remoteConfig.fetch(expiration: const Duration(seconds: 10));
await remoteConfig.activateFetched();
remoteConfig.getString('force_update_current_version');
double nVersion = double.parse(remoteConfig
.getString('force_update_current_version')
.trim()
.replaceAll(".", ""));
if(nVersion > currentVersion){
showDialog(
context: context,
builder: (_) => new AlertDialog(
title: new Text("You are not up to date"),
content: new Text("To use the application, you must update it"),
actions: <Widget>[
FlatButton(
child: Text('Go To Store'),
onPressed: () {
_launchURL(PLAY_STORE_URL);
},
)
],
)
);
}
} on FetchThrottledException catch (exception) {
print(exception);
} catch (exception) {
print(
'Unable to fetch remote config. Cached or default values will be used');
}
}
_launchURL(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
#override
Widget build(BuildContext context) {
return ScopedModelDescendant<UserModel>(builder: (context, child, model) {
return new SplashScreen(
seconds: 4,
navigateAfterSeconds:
model.isSignedIn ? HomePage() : SuggestLoginPage(),
image: new Image.asset('assets/images/logo.png', fit: BoxFit.cover,),
backgroundColor: Color(0xff131921),
styleTextUnderTheLoader: new TextStyle( ),
photoSize: 100.0,
loaderColor: Colors.white
);
});
}
}
Create a splash screen when the app running first time show your logo or a loader.
class SplashScreen extends StatefulWidget {
#override
_SplashScreenState createState() => _SplashScreenState();
}
cclass _SplashScreenState extends State<SplashScreen> {
#override
void initState() {
super.initState();
SchedulerBinding.instance.addPostFrameCallback((_) {
checkVersion();
});
}
Future checkVersion({String payload}) async {
var upToDate = false;
if (upToDate)
print('Navigate to Home');
else
return showDialog(
context: context,
builder: (_) => new AlertDialog(
title: new Text("You are not up to date"),
content: new Text("To use the application, you must update it"),
actions: <Widget>[
FlatButton(
child: Text('Go To Store'),
onPressed: () {
//navigate to store
},
)
],
)
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Stackowerflow",
style: TextStyle(
fontFamily: "Josefin",
fontSize: 55,
fontWeight: FontWeight.bold,
color: Colors.grey,
),
),
Image.asset('assets/images/splash.gif',width: 500,
height: 500,),
],
),
),
);
}
Don't forget to run this splash screen into main.dart
Now you can add your function into checkVersion and if up to date then return home or whatever u want to redirect but if it is not then redirect to the store

Screen selector on initial launch in Flutter

Let me explain first.
I have three screens in my app. These are MyHome.dart, OtherHome.dart and Selector.dart.
I want to launch Selector screen on the initial launch. In the Selector screen, there are two options to users. One is MyHome and another is OtherHome. After the first launch, the app will always open the last selected screen by the user on the first launch. What will be the right code for this?
Main.dart:
import 'selector.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Selector(),
));
}
Selector.dart:
import 'package:device_monitor/home.dart';
import 'package:flutter/material.dart';
import 'home.dart';
import 'myhome.dart';
class Selector extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyHome()),
);
},
child: Text('My Device'),
),
SizedBox(height: 30),
RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Home()),
);
},
child: Text('Others Device'),
),
],
),
);
}
}
Here a code that can help you:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(MaterialApp(
home: Selector(),
));
}
You need to get sharedpreferences package, here's a link
class Selector extends StatefulWidget {
#override
_SelectorState createState() => _SelectorState();
}
class _SelectorState extends State<Selector> {
bool pageReady = false;
/// This checks the whether page has been selected earlier,
/// should be placed in an initstate function
_checkPages() async {
SharedPreferences local = await SharedPreferences.getInstance();
if(local.getString('page-selected') != null){
if(local.getString('page-selected') == "1"){
//navigate to MyHome
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyHome()),
);
} else {
//Navigate to Home
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Home()),
);
}
} else {
setState(() {
pageReady = true;
});
}
}
#override
void initState() {
// TODO: implement initState
super.initState();
_checkPages();
}
savePage(String type) async {
if(type == "1"){
SharedPreferences local = await SharedPreferences.getInstance();
local.setString('page-selected', type);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyHome()),
);
} else {
SharedPreferences local = await SharedPreferences.getInstance();
local.setString('page-selected', type);
Navigator.push(
context,
MaterialPageRoute(builder: ( context ) => Home()),
);
}
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: pageReady ? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () {
savePage("1");
},
child: Text('My Device'),
),
SizedBox(height: 30),
RaisedButton(
onPressed: () {
savePage("2");
},
child: Text('Others Device'),
),
],
) : Center(child: CircularProgressIndicator()),
);
}
}
class MyHome extends StatefulWidget {
#override
_MyHomeState createState() => _MyHomeState();
}
class _MyHomeState extends State<MyHome> {
#override
Widget build(BuildContext context) {
return Container();
}
}
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container();
}
So, I changed Selector() to a stateful widget and used an initState() to check if the user already selected a page previously, if yes, it routes the user to that page else it opens the selector page and once the user selects a page I save the page in session also with the savePage() function.