Firebase fails to initialise on release build, Flutter - android

Am new to flutter and was trying around Firebase, but the app doesn't initialize Firebase on release
it throws an error, yet it works wen you just run the app. Am using android studio
** E/flutter (12950): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: MissingPluginException(No implementation found
for method Firebase#initializeC ore on channel
plugins.flutter.io/firebase_core)
E/flutter (12950): #0 MethodChannel._invokeMethod
(package:flutter/src/services/platform_channel.dart:156) E/flutter
(12950): E/flutter (12950): #1
MethodChannel.invokeListMethod
(package:flutter/src/services/platform_channel.dart:344) E/flutter
(12950):
E/flutter (12950): #2 MethodChannelFirebase._initializeCore
(package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:30)
E/flutter (12950):
E/flutter (12950): #3 MethodChannelFirebase.initializeApp
(package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:75)
E/flutter (12950):
E/flutter (12950): #4 Firebase.initializeApp
(package:firebase_core/src/firebase.dart:44) E/flutter (12950):
E/flutter (12950): #5 main
(package:ug_campus_papers_example/main.dart:13) E/flutter (12950):
E/flutter (12950):
**
Line 13 is the await Firebase.initializeApp();
My Main file is
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'App1 Test',
home: StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (BuildContext context, AsyncSnapshot<User> snapshot) {
if (snapshot.hasData) {
User user = FirebaseAuth.instance.currentUser;
return Make1(user: user,);
} else
return SignInScreen();
},
),
);
}
}
The pubspec.yaml file is as below
name: ug_camp_example
description: My Flutter app 1.
version: 3.0.1+3
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
firebase_core: "^0.7.0"
firebase_storage: "^7.0.0"
firebase_auth: "^0.20.1"
cloud_firestore: "^0.16.0+1"
ug_camp:
path: ../
cupertino_icons: ^1.0.2
google_fonts:
provider:
file_picker: ^1.1.1
flutter_plugin_pdf_viewer:
sqflite:
path:
flutterwave:
flutter_launcher_icons: ^0.7.4
dev_dependencies:
flutter_test:
sdk: flutter
flutter_icons:
image_path: "assets/images/apptest1.png"
android: true
ios: true
flutter:
uses-material-design: true

Please try the following:
Update your firebase packages to the latest versions. You're very outdated.
Ensure you've added your google-services.json to the project.
You mentioned you are using Android Studio, although are you running on Android or iOS? I can provide more instructions depending on the platform.

Related

Splash screen stuck and never shows the main.dart Flutter

After running my app, it's showing the splash screen and never goes away. Even if I close & reboot the app and even I run the flutter run by Flutter run lib/main.dart is showing nothing only but the splash screen , I also added the WidgetsFlutterBinding.ensureInitialized(); but still nothing is working for me. trying everything like flutter clean & flutter pub get I haven't use any package for the splash-screen. plz can anyone identify the problem would appriciated.
I've added my terminal below after launching the app.
Thanks again.
W/FlutterActivityAndFragmentDelegate(18992): A splash screen was provided to Flutter, but this is deprecated. See flutter.dev/go/android-splash-migration for migration steps.
E/flutter (18992): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null return value., null, null)
E/flutter (18992): #0 FirebaseCoreHostApi.optionsFromResource (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:246:7)
E/flutter (18992): <asynchronous suspension>
E/flutter (18992): #1 MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:89:25)
E/flutter (18992): <asynchronous suspension>
E/flutter (18992): #2 Firebase.initializeApp (package:firebase_core/src/firebase.dart:43:31)
E/flutter (18992): <asynchronous suspension>
E/flutter (18992): #3 main (package:pdfscanner/main.dart:7:3)
E/flutter (18992): <asynchronous suspension>
E/flutter (18992):
my main.dart looks like this:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: "Document Scanner",
home: HomePage(),
),
);
}
this is my build.gradle in android mainstream
buildscript {
ext.kotlin_version = '1.6.10'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.15'
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
here for the 1st error on the terminal says
A splash screen was provided to Flutter, but this is deprecated.
See flutter.dev/go/android-splash-migration for migration steps.
this was solved by removing <meta-data/> snippet on AndroidManifest.xml , which is :
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="#style/NormalTheme"
/>
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="#mipmap/launch_image"
/>
and for these part of error on terminal , which is :
E/flutter (18992): #0 FirebaseCoreHostApi.optionsFromResource (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:246:7)
E/flutter (18992): <asynchronous suspension>
E/flutter (18992): #1 MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:89:25)
E/flutter (18992): <asynchronous suspension>
E/flutter (18992): #2 Firebase.initializeApp (package:firebase_core/src/firebase.dart:43:31)
E/flutter (18992): <asynchronous suspension>
E/flutter (18992): #3 main (package:pdfscanner/main.dart:7:3)
E/flutter (18992): <asynchronous suspension>
E/flutter (18992):
I just have to remove
await Firebase.initializeApp();
which is right after the line
WidgetsFlutterBinding.ensureInitialized();
on main.dart , and by doing so I can see my main home_page.dart again. Everything works fine.

Image Not Loading in flutter android

I tried everything but nothing seems to work. According to me, the path of the image is correct. (though I have attached an image for reference). This is the error I am getting -
════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: start.png
When the exception was thrown, this was the stack
#0 PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:225
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:668
#2 AssetBundleImageProvider.load
package:flutter/…/painting/image_provider.dart:651
#3 ImageProvider.resolveStreamForKey.<anonymous closure>
package:flutter/…/painting/image_provider.dart:504
...
Image provider: AssetImage(bundle: null, name: "start.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#2bd13(), name: "start.png", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════
this is **pubspec.yaml*-
name: foodfast
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
splashscreen: ^1.3.5
cupertino_icons: ^1.0.0
firebase_auth: ^0.20.0+1
cloud_firestore: ^0.16.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/start.png
sign_in.dart
import 'package:flutter/material.dart';
class SignIn extends StatefulWidget {
#override
_SignInState createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text('sign in'),
),
backgroundColor: Colors.white,
body: Center(
child: ListView(
children: <Widget>[
Image(image: AssetImage('start.png'), fit: BoxFit.cover),
new Text('text below image')
],
)),
);
}
}
You need to add your path of image first i.e. assets and then add image name
Image(image: AssetImage('assets/start.png'), fit: BoxFit.cover),
Provide the full path even if you have already given it in pubspec.yaml.
Hence, it should be 'assets/start.png'
Use full path like 'assets/start.png' in your image path.
Image(image: AssetImage('assets/start.png'), fit: BoxFit.cover),
Try changing the path from
AssetImage("/image/start.png")
to
AssetImage("./image/start.png")
(. means go to the highest folder level)
This worked for me!

flutter url_launcher plugin issue for android release variant

When trying to run a url on browser with url_launcher plugin, I am getting this error on the release build on android. IOS and the android debug variant works fine.
E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method canLaunch on channel plugins.flutter.io/url_launcher)
#0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:157)
<asynchronous suspension>
#1 canLaunch (package:url_launcher/url_launcher.dart:124)
<asynchronous suspension>
#2 _HomeWidgetState.launchURL (package:pool_inspection/src/pages/home/home.dart:323)
<asynchronous suspension>
Here's my code
DialogButton(
color: Color(0xFF0B9CDA),
child: Text(
"Login Now",
style: TextStyle(
color: Colors.white, fontSize: 18),
),
onPressed: () {
setState(() {
launchURL();
});
},
)
Method for url launch
Future<void> launchURL() async {
const url = 'https://sometesturl.com';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
url_launcher version
url_launcher: ^5.7.10
Whats the issue I am unable to trace. I have already tried flutter clean,flutter pub get, Invalidate Cache/Restart as suggested by other answers.

Flutter Android Release Build CRASH - FirebaseCore Initialize App Issue

Currently creating an app with Flutter.
I have created an issue on the official github but figured I'd ask here too if thats cool.
Steps To Reproduct:
flutter run --release
Install on Android device (reproduced on multiple)
App will hang when attempting Firebase.initializeApp()
NOTE
I do not personally think this is a firebase issue but when we all had to update our app for AndroidX compatibility and firebase_message also called for us to create the Application.kt file to implement it's newest pubs (which I need to use due to other dependencies).
ANY HELP WOULD BE GREATLY APPRECIATED. I really think this has something to do with the Kotlin files. I say that BECAUSE I MOVED THE AWAIT FIREBASE>INITAPP call in the main.dart file lower, and I got the same kind of output but instead referencing the SharedPrefs pub I was using.
Make sense?
What do YOUR Kotlin files look like?
Main.Dart File:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await SharedPreferences.getInstance();
Admob.initialize();
await Admob.requestTrackingAuthorization();
DynamicLinkService.handleDynamicLinks();
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitDown, DeviceOrientation.portraitUp]).then(
(_) {
runApp(
StringConstants(
child: AuthProvider(auth: Auth(), child: MyApp()),
),
);
},
);
}
pubspec.yaml
version: 1.0.0+29
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
firebase_core: "^0.5.3"
# Newly reworked plugins covered by this migration guide:
firebase_auth: "^0.18.4"
cloud_firestore: "^0.14.4"
cloud_functions: "^0.7.2"
firebase_storage: "^5.2.0"
firebase_messaging: ^7.0.3 # Updated to work with new core only plugins (no new changes):
firebase_admob: "^0.10.3"
firebase_analytics: "^6.3.0"
firebase_dynamic_links: "^0.6.3"
Output:
E/flutter (27399): [ERROR:flutter/lib/ui/ui_dart_state.cc(184)] Unhandled Exception: MissingPluginException(No implementation found for method Firebase#initializeCore on channel plugins.flutter.io/firebase_core)
E/flutter (27399): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:160)
E/flutter (27399): <asynchronous suspension>
E/flutter (27399): #1 MethodChannel.invokeListMethod (package:flutter/src/services/platform_channel.dart:348)
E/flutter (27399): <asynchronous suspension>
E/flutter (27399): #2 MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:30)
E/flutter (27399): <asynchronous suspension>
E/flutter (27399): #3 MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:75)
E/flutter (27399): <asynchronous suspension>
E/flutter (27399): #4 Firebase.initializeApp (package:firebase_core/src/firebase.dart:43)
E/flutter (27399): <asynchronous suspension>
E/flutter (27399): #5 main (package:instapray/main.dart:20)
E/flutter (27399): <asynchronous suspension>
E/flutter (27399):
MainActivity.kt:
class MainActivity: FlutterActivity() {
}
Application.kt:
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
class Application:FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this)
}
override fun registerWith(registry:PluginRegistry) {
io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
}
This involved an update to the core Firebase pubs. Thanks.

Send platform message to Android method from background flutter isolate

I want to have a dart background service running forever (isolates) which will communicate with a server through websockets. And I have an API for Android which gather information to send to the server.
How can I invoke that Android methods which use callbacks and everything from the background isolate?
EDIT
So far in dart I created an Isolate to periodically invoke the poolSong method in background even if the user is using another app or has the screen turned off.
But that gives me the error below...on the github issues they say that I can't send a platform message from a different Isolate unless it's the main. But if I do it from the main isolate, when the user exit the app that isolate will terminate too.
MainDart
class _MyHomePageState extends State<MyHomePage> {
final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
static const platform = const MethodChannel('mainService');
static _poolSong(SendPort sendPort) async {
const oneSec = const Duration(seconds:1);
new Timer.periodic(oneSec, (Timer t) => platform.invokeMethod('poolSong'));
}
#override
void initState() {
super.initState();
ReceivePort receivePort = ReceivePort();
Isolate.spawn(_poolSong, receivePort.sendPort);
}
·
·
·
MainActivityJava
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
(call, result) -> {
if (call.method.equals("startService"))
startService();
if (call.method.equals("poolSong"))
poolSong();
}
);
}
ERROR
E/flutter (25412): [ERROR:flutter/runtime/dart_isolate.cc(717)] Isolate (765499726) 'main.dart:_poolSong()' exited with an error
E/flutter (25412): [ERROR:flutter/shell/common/shell.cc(186)] Dart Error: Unhandled exception:
E/flutter (25412): error: native function 'Window_sendPlatformMessage' (4 arguments) cannot be found
E/flutter (25412): #0 Window.sendPlatformMessage (dart:ui/window.dart:811:9)
E/flutter (25412): #1 BinaryMessages._sendPlatformMessage (package:flutter/src/services/platform_messages.dart:39:15)
E/flutter (25412): #2 BinaryMessages.send (package:flutter/src/services/platform_messages.dart:87:12)
E/flutter (25412): #3 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:286:49)
E/flutter (25412): <asynchronous suspension>
E/flutter (25412): #4 _MyHomePageState._poolSong.<anonymous closure> (package:musictie/main.dart:37:54)
E/flutter (25412): #5 _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)
E/flutter (25412): #6 _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
E/flutter (25412): #7 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
This error happens when trying to run dart code when the app is in the garbage (app closed), Until now the best way for dealing with background service is to do what you need in native code, and dont try to run anything from dart when app closed.
Since Flutter 3.7 plugins and platform channels can be used from any isolate, not only from the root one.
References:
https://medium.com/flutter/introducing-background-isolate-channels-7a299609cad8
https://github.com/flutter/flutter/issues/13937

Categories

Resources