I got some problem with _showSnackbar
I see following error:
Exception has occurred.
NoSuchMethodError (NoSuchMethodError: The method 'showSnackBar'
was called on null.Receiver: null
Tried calling: showSnackBar(Instance of 'SnackBar'))
But I'm sure I already created GlobalKey for ShowSnackbar :
final _scaffoldKey = GlobalKey<ScaffoldState>();
This is my function showsnackbar
void _showSnackBar(BuildContext context, String message, Color color) {
final snackBar = SnackBar(
duration: Duration(seconds: 1),
backgroundColor: color,
content: Text(message),
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
This is how I call the showsnackbar
upload != 0 ? _showSnackBar(context,"Success Upload",Colors.green) : _showSnackBar(context,"Success Upload",Colors.green);
And this is the full source code my view
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mosque/api/api_mosque.dart';
class UploadImage extends StatefulWidget {
@override
_UploadImageState createState() => _UploadImageState();
}
class _UploadImageState extends State<UploadImage> {
final _scaffoldKey = GlobalKey<ScaffoldState>();
ApiHelper api = ApiHelper();
File _image;
TextEditingController _txtNameImage = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_left),
onPressed: () => Navigator.pop(context, false),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.file_upload),
onPressed: () {
uploadImageRevisi(_image,_txtNameImage);
},
)
],
),
body: _formUpload(),
);
}
Widget _formUpload() {
return SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
children: <Widget>[
TextField(
controller: _txtNameImage,
keyboardType: TextInputType.text,
decoration: InputDecoration(hintText: "Nama Image"),
maxLength: 9,
textAlign: TextAlign.center,
),
SizedBox(
height: 50.0,
),
Container(
child: _image == null
? Text('No Images Selected')
: Image.file(_image),
),
SizedBox(
height: 50.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Icon(Icons.camera),
onPressed: () => getImageCamera(),
),
SizedBox(
width: 50.0,
),
RaisedButton(
child: Icon(Icons.image),
onPressed: () => getImageGallery(),
)
],
)
],
),
);
}
void uploadImageRevisi(File imageFile, TextEditingController _txtName) async{
int upload = await api.uploadImageRevisi(imageFile,_txtName);
upload != 0 ? _showSnackBar(context,"Success Upload",Colors.green) : _showSnackBar(context,"Success Upload",Colors.green);
}
getImageGallery() async {
var imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = imageFile;
});
}
getImageCamera() async {
var imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
_image = imageFile;
});
}
void _showSnackBar(BuildContext context, String message, Color color) {
final snackBar = SnackBar(
duration: Duration(seconds: 1),
backgroundColor: color,
content: Text(message),
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
}
Did I make a mistake somewhere?
Thanks
You've forgot to set key
property:
return Scaffold(
key: _scaffoldKey ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With