我不断收到由小部件库错误捕获的异常。我的应用程序仍然可以正常加载和运行,但是此错误使我的应用程序进行红色筛选仅需不到一秒钟的时间,然后才能正常加载我的应用程序。
我不知道,并尝试使用一些代码来修改它以及Google。在这种问题上,我在互联网上还没有找到任何东西。从技术上讲,它仍然有效,这就是为什么我感到困惑。
Performing hot restart...
Syncing files to device SM A505YN...
Restarted application in 2,533ms.
D/AndroidAudioRecorder( 1091): handleHasPermission true
I/flutter ( 1091): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 1091): The following NoSuchMethodError was thrown building AudioButton(dirty, state:
I/flutter ( 1091): AudioButtonState#41f3c):
I/flutter ( 1091): The getter 'duration' was called on null.
I/flutter ( 1091): Receiver: null
I/flutter ( 1091): Tried calling: duration
I/flutter ( 1091):
I/flutter ( 1091): The relevant error-causing widget was:
I/flutter ( 1091): AudioButton file:///C:/Users/lukem/Documents/FlutterProjects/mimir_app/lib/pages/home.dart:20:13
I/flutter ( 1091):
I/flutter ( 1091): When the exception was thrown, this was the stack:
I/flutter ( 1091): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
I/flutter ( 1091): #1 AudioButtonState.build (package:mimirapp/pages/audio/AudioButton.dart:123:25)
I/flutter ( 1091): #2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28)
I/flutter ( 1091): #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15)
I/flutter ( 1091): #4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
I/flutter ( 1091): #5 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
I/flutter ( 1091): #6 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4525:5)
I/flutter ( 1091): #7 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4710:11)
I/flutter ( 1091): #8 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
I/flutter ( 1091): ... Normal element mounting (19 frames)
I/flutter ( 1091): #27 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
I/flutter ( 1091): #28 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5991:32)
I/flutter ( 1091): ... Normal element mounting (210 frames)
I/flutter ( 1091): #238 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
I/flutter ( 1091): #239 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5991:32)
I/flutter ( 1091): ... Normal element mounting (267 frames)
I/flutter ( 1091): #506 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
I/flutter ( 1091): #507 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
I/flutter ( 1091): #508 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1174:16)
I/flutter ( 1091): #509 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1145:5)
I/flutter ( 1091): #510 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1087:17)
I/flutter ( 1091): #511 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2620:19)
I/flutter ( 1091): #512 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1086:13)
I/flutter ( 1091): #513 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:927:7)
I/flutter ( 1091): #514 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:908:7)
I/flutter ( 1091): (elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
I/flutter ( 1091):
I/flutter ( 1091): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 1091): Instance of 'Recording'
I/flutter ( 1091): RecordingStatus.Initialized
我有一个main.dart文件,该文件可访问我的Home小部件,该音频小部件可将audiobutton插入我的体内。这些类如下所示。
主文件:
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Mimir'),
centerTitle: true,
backgroundColor: Colors.green[300],
),
body: AudioButton(),
floatingActionButton: FloatingActionButton(
onPressed: () {
print("help is on it's way");
showHelpDialog(context);
},
child: Icon(Icons.help),
backgroundColor: Colors.green[400],
),
//bottomNavigationBar - used for navigating to new pages
);
}
}
AudioButton
class AudioButton extends StatefulWidget {
final LocalFileSystem localFileSystem;
AudioButton({localFileSystem})
: this.localFileSystem = localFileSystem ?? LocalFileSystem();
@override
State<StatefulWidget> createState() => new AudioButtonState();
}
class AudioButtonState extends State<AudioButton> {
List buttonPadding = [30.0,10.0,30.0,10.0];
double buttonFontSize = 16.0;
FlutterAudioRecorder _recorder;
Recording _current;
RecordingStatus _currentStatus = RecordingStatus.Unset;
int length_recording = 120;
@override
void initState() {
// TODO: implement initState
super.initState();
_init();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
//Init/record button
RaisedButton(
onPressed: () {
switch (_currentStatus) {
case RecordingStatus.Initialized:
{
_start();
break;
}
// case RecordingStatus.Recording:
// {
// _pause();
// break;
// }
// case RecordingStatus.Paused:
// {
// _resume();
// break;
// }
case RecordingStatus.Stopped:
{
_init();
break;
}
default:
break;
}
},
child: _buildText(_currentStatus),
padding: EdgeInsets.fromLTRB(buttonPadding[0],buttonPadding[1],buttonPadding[2],buttonPadding[3]),
elevation: 5.0,
color: Colors.green[500],
splashColor: Colors.green[200],
),
//Stop button
RaisedButton(
onPressed: () {
//t.cancel();
_stop();
},
child: Text(
"Stop",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: buttonFontSize,
color: Colors.white,
letterSpacing: 1.0,
)
),
padding: EdgeInsets.fromLTRB(buttonPadding[0],buttonPadding[1],buttonPadding[2],buttonPadding[3]),
elevation: 5.0,
color: Colors.green[500],
splashColor: Colors.green[200],
),
//play button
RaisedButton(
onPressed: onPlayAudio,
child: Text(
"Play",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: buttonFontSize,
color: Colors.white,
letterSpacing: 1.0,
)
),
padding: EdgeInsets.fromLTRB(buttonPadding[0],buttonPadding[1],buttonPadding[2],buttonPadding[3]),
elevation: 5.0,
color: Colors.green[500],
splashColor: Colors.green[200],
),
],
),
SizedBox(
height: 20.0,
),
//timer text variable
new Text(
"${_current.duration.inSeconds.toString()}",
style: TextStyle(
fontSize: 60.0,
),
),
//anything that helps to debug eg recording status
SizedBox(
height: 50.0,
),
new Text("Status : $_currentStatus"),
],
);
}
_init() async {
try {
if (await FlutterAudioRecorder.hasPermissions) {
String customPath = '/flutter_audio_recorder_';
io.Directory appDocDirectory;
// io.Directory appDocDirectory = await getApplicationDocumentsDirectory();
if (io.Platform.isIOS) {
appDocDirectory = await getApplicationDocumentsDirectory();
} else {
appDocDirectory = await getExternalStorageDirectory();
}
// can add extension like ".mp4" ".wav" ".m4a" ".aac"
customPath = appDocDirectory.path +
customPath +
DateTime.now().millisecondsSinceEpoch.toString();
// .wav <---> AudioFormat.WAV
// .mp4 .m4a .aac <---> AudioFormat.AAC
// AudioFormat is optional, if given value, will overwrite path extension when there is conflicts.
_recorder =
FlutterAudioRecorder(customPath, audioFormat: AudioFormat.WAV);
await _recorder.initialized;
// after initialization
var current = await _recorder.current(channel: 0);
print(current);
// should be "Initialized", if all working fine
setState(() {
_current = current;
_currentStatus = current.status;
print(_currentStatus);
});
} else {
Scaffold.of(context).showSnackBar(
new SnackBar(content: new Text("You must accept permissions")));
}
} catch (e) {
print(e);
}
}
_start() async {
try {
int count = 0;
await _recorder.start();
var recording = await _recorder.current(channel: 0);
setState(() {
_current = recording;
});
const tick = const Duration(seconds: 1);
new Timer.periodic(tick, (Timer t) async {
count++;
if (_currentStatus == RecordingStatus.Stopped) {
t.cancel();
}
// if (count > length_recording) {
// t.cancel();
// _stop();
// }
var current = await _recorder.current(channel: 0);
// print(current.status);
setState(() {
_current = current;
_currentStatus = _current.status;
});
});
} catch (e) {
print(e);
}
}
_resume() async {
await _recorder.resume();
setState(() {});
}
_pause() async {
await _recorder.pause();
setState(() {});
}
_stop() async {
var result = await _recorder.stop();
print("Stop recording: ${result.path}");
print("Stop recording: ${result.duration}");
File file = widget.localFileSystem.file(result.path);
print("File length: ${await file.length()}");
setState(() {
_current = result;
_currentStatus = _current.status;
});
}
onPlayAudio() async {
AudioPlayer audioPlayer = AudioPlayer();
await audioPlayer.play(_current.path, isLocal: true);
}
Widget _buildText(RecordingStatus status) {
var text = "";
switch (_currentStatus) {
case RecordingStatus.Initialized:
{
text = 'Start';
break;
}
case RecordingStatus.Recording:
{
text = 'Recording';
break;
}
case RecordingStatus.Paused:
{
text = 'Resume';
break;
}
case RecordingStatus.Stopped:
{
text = 'Reset';
break;
}
default:
break;
}
return Text(text,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: buttonFontSize,
color: Colors.white,
letterSpacing: 1.0,
));
}