Flutter调用showTimePicker函数会导致错误:在null上调用了getter'modalBarrierDismissLabel'

时间:2018-09-27 21:20:41

标签: dart flutter

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("Hello Flutter"),),
        body: Column(children: [
          GestureDetector(
              onTap: () {
                showTimePicker(
                  context: context,
                  initialTime: const TimeOfDay(hour: 7, minute: 0),
                );
              },
              child: Text("22:30")
          )

试图像这样向我的Flutter应用程序添加timePicker。轻敲文本会导致错误。

  

E / flutter(23737):[错误:flutter / shell / common / shell.cc(181)] Dart   错误:未处理的异常:E / flutter(23737):NoSuchMethodError:The   getter'modalBarrierDismissLabel'在null上被调用。电子/颤振   (23737):接收方:空E / flutter(23737):尝试致电:   modalBarrierDismissLabel E / flutter(23737):#0
  Object.noSuchMethod(dart:core / runtime / libobject_patch.dart:48:5)   E / flutter(23737):#1 showDialog   (包装:flutter / src / material / dialog.dart:615:53)E / flutter(23737):

1 个答案:

答案 0 :(得分:2)

这是一个问题,因为传递到showTimePicker()的上下文在小部件树中的MaterialApp上方。

要解决此问题,您需要另一个上下文,并且可以在Builder下面使用MaterialApp

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Hello Flutter"),
        ),
        body: Builder(
          builder: (context) => Column(
            children: [
              GestureDetector(
                onTap: () {
                  showTimePicker(
                    context: context,
                    initialTime: const TimeOfDay(hour: 7, minute: 0),
                  );
                },
                child: Text("22:30"),
              )
            ],
          ),
        ),
      ),
    );
  }
}