My flutter application has somewhat heavy model initialization. So it is created in async function. And future produced by this function is passed to a widget. While the model loads - I would like to display progress indicator. The problem is when I launch my app from Android Studio - progress indicator freezes and app never displays anything else! It is worth mentioning that sometimes it works. But very rare to be honest.
This reproduces both on android emulator and real device. It never freezes if launched from the device itself. It also never freezes if launched in debug mode from Android Studio.
I have created a minimal reproducing sample:
import 'package:flutter/material.dart';
Future<int> getTheAnswer() async {
// To emulate long running method
await Future.delayed(Duration(seconds: 10));
return 42;
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
var theAnswer = getTheAnswer();
// This is just to show that the future was indeed completed
theAnswer.then((value) => print("Future completed with $value"));
runApp(MyApp(theAnswer));
}
class MyApp extends StatelessWidget {
final Future<int> _myFuture;
MyApp(this._myFuture);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: Center(
child: FutureBuilder<int>(
future: _myFuture,
builder: (context, snapshot) {
if (!snapshot.hasData) {
print("No data");
return CircularProgressIndicator();
}
// This part is not executed
print("Has data");
return Text(
"The Answer to the Ultimate Question of Life, the Universe, and Everything is ${snapshot.data}");
},
))),
);
}
}
This outputs:
Launching lib\main.dart on SM G960F in debug mode...
Running Gradle task 'assembleDebug'...
√ Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk... 5.4s
Waiting for SM G960F to report its views...
Debug service listening on ws://127.0.0.1:54422/RHSBDMQJZjA=/ws
Syncing files to device SM G960F...
I/flutter (12538): No data
I/flutter (12538): Future completed with 42
And flutter doctor -v:
[√] Flutter (Channel master, 1.19.0-2.0.pre.133, on Microsoft Windows [Version 10.0.19041.208], locale en-US)
• Flutter version 1.19.0-2.0.pre.133 at C:\Repos\flutter
• Framework revision e92afc16b6 (13 hours ago), 2020-05-21 05:29:01 +0900
• Engine revision 2d4e83921d
• Dart version 2.9.0 (build 2.9.0-9.0.dev 40f7a11d89)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at C:\Users\am580\AppData\Local\Android\sdk
• Platform android-29, build-tools 29.0.2
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
• All Android licenses accepted.
[√] Android Studio (version 3.5)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 42.1.1
• Dart plugin version 191.8593
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
[√] IntelliJ IDEA Community Edition (version 2019.3)
• IntelliJ at C:\Users\am580\AppData\Local\JetBrains\Toolbox\apps\IDEA-C\ch-0\193.6911.18
• Flutter plugin version 45.0.2
• Dart plugin version 193.6911.31
[√] Connected device (2 available)
• SM G960F • 21f9139c290c7ece • android-arm64 • Android 10 (API 29)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 10 (API 29) (emulator)
• No issues found!