I want to upload files in the firebase storage, but i have an error of appcheck after many researches, i saw that i have to activate Appcheck on firebase, but also to activate it on my application
in a google video on youtube, i have seen that i have to call this function while building my app:
initFirebase(){
FirebaseApp.initializeApp(/*context*/ this);
FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
firebaseAppCheck.installAppCheckProviderFactory(
SafetyNetAppCheckProviderFactory.getInstance());
}
But i have errors, that the methods "initializeApp";"getInstance" and "installAppCheckProviderFactory" are not defined,
Your MainActivity.kt shoul look like this:
package com.example.app //your package name
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import com.google.firebase.FirebaseApp
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory
class MainActivity: FlutterActivity() {
private val CHANNEL = "samples.flutter.dev/battery"
override fun configureFlutterEngine(#NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
FirebaseApp.initializeApp(/*context=*/this)
val firebaseAppCheck = FirebaseAppCheck.getInstance()
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactory.getInstance()
)
}
}
}
And your main.dart like this:
import 'package:firebase_app_check/firebase_app_check.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseAppCheck.instance.activate();
}
Related
I have a situation where I need to use local_auth which expects FlutterFragmentActivity as main activity, but on the other hand I also wanna use video_player package which expects FlutterActivity as main activity.
Main Activity for local_auth:
package //your package name for eg.com.example.filename
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterFragmentActivity() {
override fun configureFlutterEngine(#NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
}
Main Activity for video_player:
package //your package name for eg.com.example.filename
import android.content.Context
import android.os.Bundle
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.videoplayer.*
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(#NonNull flutterEngine:
FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
flutterEngine.getPlugins().add(VideoPlayerPlugin())
}
}
Is there a way to combine both activities so that both packages get their own required FlutterActivity and FlutterFragmentActivity for functioning?
If I use main activity as FlutterFragmentActivity app crashes while compiling and video_player package works smoothly, and if I use FlutterActivity app compiles successfully, but local_auth didn't worked because it requires FlutterFragmentActivity for auth apis to function.
It's my first time testing an app I tried to test firebase auth and had this error
I also search in the doc but everything is outdated or doesn't implement firebase
This is my code
import 'package:ecommerce/main.dart';
import 'package:ecommerce/services/auth.dart';
import 'package:fake_cloud_firestore/fake_cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:rxdart/subjects.dart';
class MockFirebaseAuth extends Mock implements FirebaseAuth {}
class MockFirebaseUser extends Mock implements User {}
class MockUserCredential extends Mock implements Future<UserCredential> {}
void main() {
MockFirebaseAuth _auth = MockFirebaseAuth();
MockUserCredential mockUserCredential = MockUserCredential();
AuthService authManager = AuthService();
final instance = FakeFirebaseFirestore();
group("User Auth Test", () {
test('Returns no user if not signed in', () async {
final auth = MockFirebaseAuth();
final user = auth.currentUser;
expect(user, isNull);
});
});
}
I'm using the Bloc state mangement Library -
currently the version of the Library is 8.1.1
I've followed some guides and tutorials online , but I encountered an error regarding the reference to "this" in AddItem event functions below:
import 'dart:async';
import 'package:foodapptest/models/basket_model.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../models.dart';
import 'package:foodapptest/ResturantMenuItem.dart';
part 'basket_event.dart';
part 'basket_state.dart';
class BasketBloc extends Bloc<BasketEvent,BasketState>
//TO do fix bloc
{
BasketBloc() : super(BasketLoading()) {
on<BasketStart>(_onStartBasket);
on<ItemAdd>(_onAddItem);
}
}
Future<void> _onStartBasket(
BasketStart event,
Emitter<BasketState> emit,
) async {
emit( BasketLoading());
try{
emit(BasketLoaded(basket: Basket(),),);
}catch (e) {
print(e);
}
}
Future<void> _onAddItem(
ItemAdd event,
Emitter<BasketState> emit,
) async {
final state = this.state;
if(state is BasketLoaded) {
try {
emit(BasketLoaded(basket: state.basket.copyWith(items: List.from(state.basket.items)..add(event.item))));
} catch (_) {}
}
}
}
The error 1:
The error 2:
all helps appreciated :)
Thank you!
Problem solved: The functions _onAddItem.. should be inside the class.
import 'dart:async';
import 'package:foodapptest/models/basket_model.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../models.dart';
import 'package:foodapptest/ResturantMenuItem.dart';
part 'basket_event.dart';
part 'basket_state.dart';
class BasketBloc extends Bloc<BasketEvent,BasketState>
//TO do fix bloc
{
BasketBloc() : super(BasketLoading()) {
on<BasketStart>(_onStartBasket);
on<ItemAdd>(_onAddItem);
}
Future<void> _onStartBasket(
BasketStart event,
Emitter<BasketState> emit,
) async {
emit( BasketLoading());
try{
emit(BasketLoaded(basket: Basket(),),);
}catch (e) {
print(e);
}
}
Future<void> _onAddItem(
ItemAdd event,
Emitter<BasketState> emit,
) async {
final state = this.state;
if(state is BasketLoaded) {
try {
emit(BasketLoaded(basket: state.basket.copyWith(items: List.from(state.basket.items)..ad
d(event.item))));
} catch (_) {}
}
}
}
}
I want to play an audio as the app background music(without button click/autoplay). The code seen like no problem, but cant display.Did I do anything wrong with the code?
import 'package:flutter/material.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(GameWidget(game: Audio()));
}
class Audio extends FlameGame with TapDetector {
#override
Future<void> onLoad() async {
super.onLoad();
}
#override
void onTapUp(TapUpInfo) {
FlameAudio.bgm.play('assets/Sound3.mp3');
}
}
pubspec.yaml
assets:
- assets/Sound3.mp3
[The Error Show in C:\flutter_windows_2.10.4-stable\flutter\packages\flutter\lib\src\services\asset_bundle.dart][1]
[1]: https://i.stack.imgur.com/p383i.png
const AssetImage('icons/heart.png', package: 'my_icons');
Assets used by the package (or plugin) itself should also be fetched using the package argument.
line: https://docs.flutter.dev/development/ui/assets-and-images
I got an error for unhandled plugin exception for google_fonts and shared_preferences:
Unhandled Exception: MissingPluginException(No implementation found
for method getAll on channel plugins.flutter.io/shared_preferences)
Unhandled Exception: MissingPluginException(No implementation found
for method getApplicationSupportDirectory on channel
plugins.flutter.io/path_provider) MethodChannel.invokeMethod
(package:flutter/src/services/platform_channel.dart:332:12)
_localPath (package:google_fonts/src/file_io_desktop_and_mobile.dart:28:27)
googleFontsTextStyle
(package:google_fonts/src/google_fonts_base.dart:107:3)
import androidx.annotation.NonNull
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(#NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
}
By adding above code solved the problem it was suggested on GitHub
link
I'm solved with change code from mainactivity.kt or mainactivity.java.
import androidx.annotation.NonNull
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterFragmentActivity() {
override fun configureFlutterEngine(#NonNull flutterEngine:
FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
}
}
after that, flutter clean and run