I'm doing course on flutter. I was trying to play sound in flutter.
Sadly, I'm having below error code
Here is my complete code
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
void main() => runApp(XylophoneApp());
class XylophoneApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: TextButton(
onPressed: () async {
final player=AudioPlayer();
await player.play(AssetSource('sounds/note1.wav'));
},
child: const Text('Click Me'))),
),
),
);
}
}
Here is my pubspec.yaml file settings
dependencies:
audioplayers: ^1.1.1
assets:
- sounds/
Here is my errors:
****[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: Unable to load asset: assets/sounds/note1.wav
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:258:7)
<asynchronous suspension>
#1 AudioCache.fetchToMemory (package:audioplayers/src/audio_cache.dart:75:22)
<asynchronous suspension>
#2 AudioCache.load (package:audioplayers/src/audio_cache.dart:101:31)
<asynchronous suspension>
#3 AudioPlayer.setSourceAsset (package:audioplayers/src/audioplayer.dart:237:17)
<asynchronous suspension>
#4 AudioPlayer.play (package:audioplayers/src/audioplayer.dart:123:5)
<asynchronous suspension>
#5 XylophoneApp.build.<anonymous closure> (package:xylophone1/main.dart:16:21)
<asynchronous suspension>****
I just need to play sound with this audio player package.
I've changed pubspec.yaml settings and folder name "assets"
I also changed source this line to await "player.play(AssetSource('note1.wav'));"
Related
Hello I was calling a startelection function from my flutter app on a button click and I got this error
the startelection function connects my flutter app with my solidity smart contract and starts the election through infura
I don't know why I am getting this error it was running fine before
error:
E/flutter (31094): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'Null' is not a subtype of type 'int' in type cast
E/flutter (31094): #0 JsonRPC.call (package:web3dart/json_rpc.dart:53:27)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #1 Web3Client._makeRPCCall (package:web3dart/src/core/client.dart:54:20)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #2 Web3Client.getGasPrice (package:web3dart/src/core/client.dart:175:18)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #3 _fillMissingData (package:web3dart/src/core/transaction_signer.dart:36:16)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #4 Web3Client.signTransaction (package:web3dart/src/core/client.dart:316:26)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #5 Web3Client.sendTransaction (package:web3dart/src/core/client.dart:285:18)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #6 startElection (package:election/services/functions.dart:29:18)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #7 _HomeState.build.<anonymous closure> (package:election/pages/home.dart:53:25)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094):
home.dart :
import 'package:election/utils/Constants.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';
import '../services/functions.dart';
import 'Electioninfo.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
#override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
late Client? httpClient;
late Web3Client? ethclient;
TextEditingController controller = TextEditingController();
#override
void initState() {
httpClient = Client();
ethclient = Web3Client(infura_url, httpClient!);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Start Election'),
),
body: Container(
padding: const EdgeInsets.all(14),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: controller,
decoration: const InputDecoration(
filled: true, hintText: 'Enter election name'),
),
const SizedBox(
height: 10,
),
Container(
width: double.infinity,
height: 45,
child: ElevatedButton(
onPressed: () async {
if (controller.text.isNotEmpty) {
await startElection(controller.text, ethclient!);
if(!mounted)return;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ElectionInfo(
ethClient: ethclient!,
electionName: controller.text,
)));
}
},
child: const Text('Start Election')))
],
),
),
);
}
}
function.dart :
Future<String> startElection(String name,Web3Client ethclient) async{
var response = await callFunction('startElection', [name], ethclient, owner_private_key);
print("election started successfully");
return response;
}
Future<String> callFunction(String funcname,List<dynamic>args,Web3Client ethClient,String privateKey) async{
EthPrivateKey credentials = EthPrivateKey.fromHex(privateKey);
DeployedContract contract = await loadcontract();
final ethfunction = contract.function(funcname);
final result = ethClient.sendTransaction(
credentials,Transaction.callContract(contract: contract, function: ethfunction, parameters: args),
chainId: null,fetchChainIdFromNetworkId: true);
return result;
}
Please try with addding dart_web3: ^0.0.3 dependencies
i tried to use the Flutter_webview library, But i can't seem to get it to work. All i see is blank screen and in the Debug i see Asynchronous suspension
Can anyone help me what could be the issue?
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class NewPageClicked extends StatelessWidget {
final Completer<WebViewController> _controller =
Completer<WebViewController>();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Test"),
),
body: WebView(
initialUrl: 'https://www.google.com/',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
),
);
}
}
And this is the debug console
E/flutter (31283): #0 StandardMethodCodec.decodeEnvelope
package:flutter/…/services/message_codecs.dart:597
E/flutter (31283): #1 MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:158
E/flutter (31283): <asynchronous suspension>
E/flutter (31283): #2 TextureAndroidViewController._sendCreateMessage
package:flutter/…/services/platform_views.dart:1043
E/flutter (31283): <asynchronous suspension>
E/flutter (31283): #3 AndroidViewController.create
package:flutter/…/services/platform_views.dart:748
E/flutter (31283): <asynchronous suspension>
E/flutter (31283): #4 RenderAndroidView._sizePlatformView
package:flutter/…/rendering/platform_view.dart:193
E/flutter (31283): <asynchronous suspension>
E/flutter (31283):
I'm trying to open the camera to scan the QR code when I press the button that supposes to open the QR code camera
it returns the following Exception
E/flutter (15313): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method scanBarcode on channel flutter_barcode_scanner)
E/flutter (15313): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:157:7)
E/flutter (15313): <asynchronous suspension>
E/flutter (15313): #1 FlutterBarcodeScanner.scanBarcode (package:flutter_barcode_scanner/flutter_barcode_scanner.dart:42:28)
E/flutter (15313): <asynchronous suspension>
E/flutter (15313): #2 _ScanQRState.scanbarcode (package:restorantapp/pages/ScanQR.dart:15:15)
E/flutter (15313): <asynchronous suspension>
E/flutter (15313):
here is my code below
import 'package:flutter/material.dart';
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
class ScanQR extends StatefulWidget {
#override
_ScanQRState createState() => _ScanQRState();
}
class _ScanQRState extends State<ScanQR> {
String code = "";
String getCode = "";
Future scanbarcode() async {
getCode = await FlutterBarcodeScanner.scanBarcode(
"#009922", "CANCEL", true, ScanMode.DEFAULT);
setState(() {
code = getCode;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Scan QR Code'),
centerTitle: true,
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlatButton(
color: Colors.blue,
padding: EdgeInsets.all(16.0),
child: Text('Scan QR Code',),
onPressed: () {
print('FlatButton pressed');
scanbarcode();
},
),
Text(code)
],
),
);
}
}
I'm following a youtube tutorial in the video the app supposed to ask the user for permission but my app didn't ask for any permission I also try to add this permission to my manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
but it didn't work
!!!
Execute following commands
flutter clean
flutter pub get
Then run your app again
I created an animation using Flare for my Flutter project. But, I got an error when trying to Run it. The animation doesn't work and only a black screen with the logo i used shows.
Here is my code:
import 'package:flare_splash_screen/flare_splash_screen.dart';
import 'package:flutter/material.dart';
void main () {
runApp(MaterialApp(
home: SplashScreen(
'assets/splash.flr',
(context) => SplashTela(),
startAnimation: 'intro',
backgroundColor: Color(0xff3333),
),
));
}
class SplashTela extends StatefulWidget {
#override
_SplashTelaState createState() => _SplashTelaState();
}
class _SplashTelaState extends State<SplashTela> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: null,
body: Container(
color: Colors.redAccent,
),
);
}
}
And here is the error:
E/flutter (25496): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception:
'package:flare_splash_screen/flare_splash_screen.dart': Failed assertion: line 113 pos 16: '!
(isLoading == null && until == null)': isLoading and until are null, pick one ;)
E/flutter (25496): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:40:39)
E/flutter (25496): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter (25496): #2 new SplashScreen
(package:flare_splash_screen/flare_splash_screen.dart:113:16)
E/flutter (25496): #3 main (package:leagueuniverse/main.dart:6:11)
E/flutter (25496): #4 _runMainZoned.<anonymous closure>.<anonymous closure>
(dart:ui/hooks.dart:229:25)
E/flutter (25496): #5 _rootRun (dart:async/zone.dart:1124:13)
E/flutter (25496): #6 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (25496): #7 _runZoned (dart:async/zone.dart:1516:10)
E/flutter (25496): #8 runZoned (dart:async/zone.dart:1500:12)
E/flutter (25496): #9 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:221:5)
E/flutter (25496): #10 _startIsolate.<anonymous closure> (dart:isolate-
patch/isolate_patch.dart:305:19)
E/flutter (25496): #11 _RawReceivePortImpl._handleMessage (dart:isolate-
patch/isolate_patch.dart:172:12)
E/flutter (25496):
you need to pass until: () => Future.delayed(Duration(seconds: 5)),
or use isLoading demo code for isLoading https://github.com/jaumard/flare_splash_screen/blob/master/example/lib/isLoading.dart
an use SplashScreen.navigate
I use official example to test your case
code snippet
void main() {
runApp(MaterialApp(
home: SplashScreen.navigate(
name: 'assets/intro.flr',
next: (context) => SplashTela(),
startAnimation: '1',
backgroundColor: Color(0xff3333),
until: () => Future.delayed(Duration(seconds: 5)),
),
));
}
full test code work without error
import 'package:flare_splash_screen/flare_splash_screen.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: SplashScreen.navigate(
name: 'assets/intro.flr',
next: (context) => SplashTela(),
startAnimation: '1',
backgroundColor: Color(0xff3333),
until: () => Future.delayed(Duration(seconds: 5)),
),
));
}
class SplashTela extends StatefulWidget {
#override
_SplashTelaState createState() => _SplashTelaState();
}
class _SplashTelaState extends State<SplashTela> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: null,
body: Container(
color: Colors.redAccent,
),
);
}
}
So my ball images are showing up on my emulator but not my magic.jpg that I added myself. My pubspec.yaml is fine. I know. Does anyone know what's wrong?
my folder tree
pubspec.yaml file:
name: magic_8_ball
description: A new Flutter application.
version: 1.0.0+1
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/images/magic.jpg
my code:
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: BallPage(),
),
);
class BallPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue[900],
title: Text('Ask Me Anything'),
),
body: Stack(children: [
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/magic"),
fit: BoxFit.cover,
),
),
),
Ball()
]),
);
}
}
class Ball extends StatefulWidget {
#override
_BallState createState() => _BallState();
}
class _BallState extends State<Ball> {
int ballNum = 1;
#override
Widget build(BuildContext context) {
return Center(
child: FlatButton(
onPressed: () {
setState(() {
ballNum = Random().nextInt(5) + 1;
});
},
child: Image.asset('assets/images/ball$ballNum.png')));
}
}
error log:
I/flutter (22530): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter (22530): The following assertion was thrown resolving an image codec:
I/flutter (22530): Unable to load asset: assets/images/magic
I/flutter (22530):
I/flutter (22530): When the exception was thrown, this was the stack:
I/flutter (22530): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter (22530): <asynchronous suspension>
I/flutter (22530): #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:464:44)
I/flutter (22530): <asynchronous suspension>
I/flutter (22530): #2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:449:14)
I/flutter (22530): #3 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:316:48)
I/flutter (22530): #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
I/flutter (22530): #5 ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:316:25)
I/flutter (22530): (elided 13 frames from package dart:async)
I/flutter (22530):
I/flutter (22530): Image provider: AssetImage(bundle: null, name: "assets/images/magic")
I/flutter (22530): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#77a87(), name: "assets/images/magic",
I/flutter (22530): scale: 1.0)
I/flutter (22530): ════════════════════════════════════════════════════════════════════════════════════════════════════
Thanks a bunch!!
what my app looks like (as you can see, any ball image can show up, but not my background image, which I called magic.jpg
You didn't put the extension for the image magic in the widget AssetImage.
Simply, put it like this AssetImage("assets/images/magic.jpg") and it should work fine.