Flutter:NoSuchMethodError:方法“ showSnackBar”在null上被调用

时间:2019-08-16 08:52:37

标签: flutter dart

showSnackbar出现了一些问题;我收到错误消息:

  

发生异常。
  NoSuchMethodError(NoSuchMethodError:方法'showSnackBar'在null上调用。
  接收者:null
  尝试调用:showSnackBar('SnackBar'的实例))

但是我确定我已经为ShowSnackbar创建了 GlobalKey

final _scaffoldKey = GlobalKey<ScaffoldState>();

这是我的功能shownackbar

void _showSnackBar(BuildContext context, String message, Color color) {
    final snackBar = SnackBar(
      duration: Duration(seconds: 1),
      backgroundColor: color,
      content: Text(message),
    );
    _scaffoldKey.currentState.showSnackBar(snackBar);
  }

这就是我所说的showsnackbar

upload != 0 ? _showSnackBar(context,"Success Upload",Colors.green) : _showSnackBar(context,"Success Upload",Colors.green);

这是我认为的完整源代码

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

我在某个地方犯了错误吗?

谢谢

1 个答案:

答案 0 :(得分:3)

您忘记设置key属性:

return Scaffold(
  key: _scaffoldKey ...