Cannot upload files to Firebase Storage using Flutter in Debug mode - flutter

Hi I'm having trouble testing uploading files to Firebase Storage with Flutter using an emulator in Android Studio. I keep getting this error even though I followed the documentation: https://firebase.google.com/docs/app-check/flutter/default-providers. I also followed the one for debug mode: https://firebase.google.com/docs/app-check/flutter/debug-provider.
Error getting App Check token; using placeholder token instead. Error:
com.google.firebase.FirebaseException: 7:
Here is my MainActivity.kt
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.FirebaseApp
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory
class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
FirebaseApp.initializeApp(this)
val firebaseAppCheck = FirebaseAppCheck.getInstance()
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactory.getInstance()
)
super.onCreate(savedInstanceState)
}
}
main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Firebase.apps.isEmpty) {
await Firebase.initializeApp(
name: 'XXX',
options: DefaultFirebaseOptions.currentPlatform,
);
await FirebaseAppCheck.instance.activate(
webRecaptchaSiteKey: 'recaptcha-v3-site-key',
);
}
runApp(const MyApp());
}
I've read a lot of answers to this question but none work. I expect to see this line in my logs but I don't see it
D DebugAppCheckProvider: Enter this debug secret into the allow list
in the Firebase Console for your project:
123a4567-b89c-12d3-e456-789012345678
Please help. Thank you!

Related

Firebase crashlytics is not reporting anything on Flutter

I've initialized and configured Firebase for my Flutter project using this documentation and the instructions given by the Firebase Console. After that, I followed this crashlytics documentation for adding crashlytics to my app. But no matter what I've tried, I couldn't see any reports on the Firebase console. Still seeing this;
My main function looks like this:
Future main() async {
runZonedGuarded(
() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;
runApp(const App());
},
(error, stack) async {
await FirebaseCrashlytics.instance.recordError(error, stack);
},
);
}
I've tried to;
crash my app using FirebaseCrashlytics.instance.crash()
throw an exception and record it by the onError callback of the runZonedGuarded
manually record an exception using FirebaseCrashlytics.recordError(error, stack)
setting fatal: true or using FirebaseCrashlytics.instance.recordFlutterFatalError
Is there something that I'm missing? I'm really stuck at this point.

Unable to Initialize Firebase in my application - Flutter

I'm trying to integrate the Firebase registration in my application by using the version firebase_auth: ^3.3.6 & firebase_core: ^1.11.0, upon using this in pubspec and initializing in the app as final _auth = FirebaseAuth.instance;
Do have initialized firebase in main as
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
I'm getting an error stating as
/C:/src/flutter2.8/.pub-cache/hosted/pub.dartlang.org/firebase_auth_platform_interface-6.2.0/lib/src/method_channel/utils/exception.dart:14:11: Error: Member not found: 'Error.throwWithStackTrace'. Error.throwWithStackTrace(exception, stackTrace);
Try to upgrade your flutter using,
flutter upgrade
If this is not working than try using lower version of firebase_auth:2.0.0
I found this Link

Tried to get location before initializing timezone database

I am trying to schedule a notification:but I got this error:
Unhandled Exception: Tried to get location before initializing timezone database
Although I initialized the Timezone in the init function:
tz.initializeTimeZones();
schedule code:
Future<void> scheduleNotifications(String title,int yy,int mm,int dd,int hh,int ii) async {
final loc_egypt = tz.getLocation('Africa/Cairo');
final tz.TZDateTime now = tz.TZDateTime.now(loc_egypt);
var schedule_30before=tz.TZDateTime(loc_egypt,yy,mm,dd,hh,ii).subtract(Duration(minutes: 30));
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
title,
"The Webinar will start after 30 minutes",
schedule_30before,
NotificationDetails(android: _androidNotificationDetails),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
}
In my case the problem was with invalid assets. I am working on a Flutter Web project, so I was doing:
import 'package:timezone/browser.dart' as tz;
void main() async {
await tz.initializeTimeZone();
// other stuff
}
After digging into the source code of timezone package, I discovered that it is initialising using a *.tzf file, which I wasn't including in my project's assets (rookie's mistake). So I edited my pubspec.yaml like this:
flutter:
assets:
- packages/timezone/data/latest.tzf
# - packages/timezone/data/latest_all.tzf <- for all the db variants
# - packages/timezone/data/latest_10y.tzf <- for 10y db variant
But there was still one more thing to do. As it turns out tz.initializeTimeZone() assumes that a default path is packages/timezone/data/latest.tzf BUT my Flutter Web project was putting all the assets in assets catalogue, so the actual path was assets/packages/timezone/data/latest.tzf and I just had to pass it as an argument to tz.initializeTimeZone('assets/packages/timezone/data/$dbVariant'). Of course you need to replace $dbVariant with the variant of your choice, the one that you included in your assets.
TL;DR
Include a timezone database in assets:
flutter:
assets:
- packages/timezone/data/latest.tzf
Initialise timezone with a full path to assets:
import 'package:timezone/browser.dart' as tz;
void main() async {
await tz.initializeTimeZone('assets/packages/timezone/data/latest.tzf');
// other stuff
}
You need to import the following packages:
import 'package:timezone/data/latest.dart' as tzl;
import 'package:timezone/standalone.dart' as tz;
Then, you can call for example something like:
ElevatedButton(
onPressed: ()async {
await scheduleNotifications("title", 2022, 9, 1, 1, 47);
},
child: Text("Notify Me!"))
Given you put tzl.initializeTimeZones(); in your initState().
Your issue is in using initializeTimeZones() of a package different than "package:timezone/data/latest.dart"
In the main() method of your app, verify your imports and call the init method
//Pick one of the three following imports :
//If you just want the latest
import 'package:timezone/data/latest.dart' as tz;
//If you use the 10y db variant
import 'package:timezone/data/latest_10y.dart' as tz;
//If you want to import all the db variants
import 'package:timezone/data/latest_all.dart' as tz;
void main() {
tz.initializeTimeZones();
}
In my case I created a future method and initialized it in the main() function of the main.dart file. This is my code:
import 'package:timezone/browser.dart' as tz;
Future<void> main() async {
initTimeZone();
// add other setup here
}
void initTimeZone(){
tz.initializeTimeZones();
}
Hope this helps!

Save a memory image (such as Uint8list) as image file in flutter

I have some Uint8lists and I want to save them as jpg files.
Can anyone help?
By 'storage', do you mean write to a file? You don't really need "flutter" to do this. Just use the libraries provided by dart. Here is an example of downloading my gravatar which you can get as Uin8List and then saving it to a file.
import 'dart:io';
import 'dart:typed_data';
import 'package:http/http.dart' as http;
void main() {
http.get('https://www.gravatar.com/avatar/e944138e1114aefe4b08848a46465589').then((response) {
Uint8List bodyBytes = response.bodyBytes;
File('my_image.jpg').writeAsBytes(bodyBytes);
});
}
Here is a simple and short solution of your problem. Use this line in your code as I did:
"SettableMetadata(contentType: "image/jpeg"),"
Code:
if (kIsWeb) {
await ref.putData(
await imageFile.readAsBytes(),
SettableMetadata(contentType: "image/jpeg"),
);
url = await ref.getDownloadURL();
}

Flutter in_app_purchase '_enablePendingPurchases': enablePendingPurchases() must be called before calling startConnection

I am using the simple code below
bool available = await InAppPurchaseConnection.instance.isAvailable();
however it is returning the error
E/flutter (14525): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: 'package:in_app_purchase/src/billing_client_wrappers/billing_client_wrapper.dart': Failed assertion: line 101 pos 12: '_enablePendingPurchases': enablePendingPurchases() must be called before calling startConnection
I was wondering if anyone knew a reason for this error and if so what should i fo about it, Happy to have any suggestions - thanks.
The documentation is very thin on this and should actually be more clear. You need include the line below in main() for it to work.
void main() {
///Include this in main() so purchases are enabled
InAppPurchaseConnection.enablePendingPurchases();
runApp(MyApp());
}
I could not import InAppPurchaseConnection to try the accepted solution, and fixed this issue with the following:
import 'package:flutter/foundation.dart';
import 'package:in_app_purchase_android/in_app_purchase_android.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
// Inform the plugin that this app supports pending purchases on Android.
// An error will occur on Android if you access the plugin `instance`
// without this call.
if (defaultTargetPlatform == TargetPlatform.android) {
InAppPurchaseAndroidPlatformAddition.enablePendingPurchases();
}
runApp(MyApp());
}