FlutterError (Unable to load asset: assets/audio/assets/Sound3.mp3) - flutter

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

Related

didChaneDependencies not working / flutter

Here is code from one of the screens where I'm trying to use didChaneDependencies:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shop/screens/cart_screen.dart';
import '../widgets/products_grid.dart';
import '../widgets/badge.dart';
import '../widgets/app_drawer.dart';
import '../providers/products.dart';
import '../providers/cart.dart';
enum FilterOptions {
Favorites,
All,
}
class ProductsOverviewScreen extends StatefulWidget {
#override
_ProductsOverviewScreenState createState() => _ProductsOverviewScreenState();
}
class _ProductsOverviewScreenState extends State<ProductsOverviewScreen> {
var _showOnlyFavorite = false;
var _isInit = true;
#override
void initState() {
print('initState');
super.initState();
}
#override
void didChaneDependencies() {
print('didChaneDependencies');
super.didChangeDependencies();
}
Problem is that it does not fires up.
And the compiler highlights it with the following message:
The method doesn't override an inherited method. Try updating this
class to match the superclass, or removing the override annotation.
I did try remove the #override , but that did not fixed the problem, I cannot see
print('didChaneDependencies'); in the debugger.
Fix the spelling of the function name, You are missing a G in the didChangeDependencies function name
#override
void didChangeDependencies() {
....

How to navigate to widgets outside of a Flutter Flame Engine game widget

Is it possible to navigate out of a Flame Engine game widget into other Flutter widgets?
The app below immediately loads the game widget. How can navigation outside of the Game widget to another Flutter widget be achieved?
Flutter version: 2.2.3
Dart version: 2.13.4
Flame Engine version: flame-1.0.0-rc8
main.dart
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:my_game.dart';
void main() {
runApp(
GameWidget(
game: MyGame(),
),
);
}
my_game.dart
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/gestures.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
class MyGame extends BaseGame with TapDetector {
#override
void update(double dt) { /* TODO */ }
#override
void render(Canvas canvas) { /* TODO */ }
#override
void onTapUp(TapUpDetails details) {
// How to navigate outside of the game widget?
}
}
You can do two things:
Either put the GameWidget in a Stack and handle navigation by Navigator from Flutter and remove and add the GameWidget to the widget tree when deemed necessary. Flutter Navigation docs
Use the Overlays API in Flame to handle the state from within Flame instead. Flame docs
For using the overlays you add the overlays that you want to have accessible when you create the GameWidget and then you call game.overlays.add to render a specific widget, and game.overlays.remove to stop rendering it.
I do recommend that you upgrade from rc8 to rc13, since the docs are for that version and things are more stable in general.

Flutter BuildContext extension not working

Here is my extension file:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
extension BuildContextExtensions on BuildContext {
void snack(String text){
ScaffoldMessenger.of(this).showSnackBar(SnackBar(content: Text(text)));
}
}
When I try to call it context.snack("asd"); it shows that The method 'snack' isn't defined for the type 'BuildContext'. And if I hover on the context it says package:flutter/src/widgets/framework.dart BuildContext get context
Solution was to manually import the file with the extension class into the file where it is used. Android studio didnt suggest me to import it. If i wrote the class name, only then it suggested to import it.
Every extension in dart needs a name, try adding a name to your extension
extension CustomSnackBar on BuildContext {
void snack(String text){
ScaffoldMessenger.of(this).showSnackBar(SnackBar(content: Text(text)));
}
}

Error while handling Appcheck with flutter

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();
}

How to export and use global variable

I am creating a Flutter App and I need a global http client with some setup.
Flutter: 1.5.4
Dart: 2.3.2
// utils/http.dart
import 'dart:io';
import 'package:cookie_jar/cookie_jar.dart';
import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
Dio http = Dio();
void main() async {
// Save cookies to cookie jar.
Directory appDir = await getApplicationDocumentsDirectory();
http.interceptors.add(CookieManager(PersistCookieJar(dir: appDir.path)));
}
// main.dart
import 'package:flutter/material.dart';
import 'app.dart';
import 'utils/http.dart';
void main() {
print(http.interceptors); // returns []
runApp(App());
}
I expect that main function in http should be automatically executed.
A common solution to this is to wrap it in a Widget and place that widget in the Widget Tree. High up in the tree, so that it's sort of global. It's called "lifing state up".
The out of box solution of Flutter for such things is InheritedWidget. It's worth looking at and understand because most 3rd-party solutions rely on it.
To make life a tad easier, however, I use pacakge:provider. Code would look like so:
Creating
Provider<Dio>(
builder: (_) => Dio(),
child: MaterialApp(/*...*/)
);
Consuming
later in the widget tree:
Consumer<Dio>(
builder: (ctx, dio, _) {
// widget builder method
debugPrint('Dio instance: ${dio}');
return Container();
}
);