无法将类型为“小部件"的值分配给类型为"PreferredSizeWidget"的变量 [英] A value of type 'Widget' can't be assigned to a variable of type 'PreferredSizeWidget'

查看:85
本文介绍了无法将类型为“小部件"的值分配给类型为"PreferredSizeWidget"的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此程序中有2个错误,这些错误已显示在上面的图像中

THERE ARE 2 ERRORS IN THIS PROGRAM WHICH HAVE BEEN SHOWN IN THE IMAGES ABOVE

Main.dart文件

Main.dart file

void main() {
 
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Personal Expenses ',
      theme: ThemeData(
        primarySwatch: Colors.green,
        accentColor: Colors.amber,
        //errorColor: Colors.red[700],
        fontFamily: 'Quicksand',
        textTheme: ThemeData.light().textTheme.copyWith(
            title: TextStyle(
              fontFamily: 'OpenSans',
              fontWeight: FontWeight.bold,
              fontSize: 18,
            ),
            button: TextStyle(color: Colors.amber)),
        appBarTheme: AppBarTheme(
          textTheme: ThemeData.light().textTheme.copyWith(
                title: TextStyle(
                  fontFamily: 'OpenSans',
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
              ),
        ),
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
 
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final List<Transaction> _userTransactions = [
   
  ];
  bool _showChart = false;
  List<Transaction> get _recentTransactions {
    return _userTransactions.where((tx) {
      return tx.date.isAfter(
        DateTime.now().subtract(
          Duration(days: 7),
        ),
      );
    }).toList();
  }

  void _addNewTransaction(
      String txTitle, double txAmount, DateTime chosenDate) {
    final newTx = Transaction(
      title: txTitle,
      amount: txAmount,
      date: chosenDate,
      id: DateTime.now().toString(),
    );

    setState(() {
      _userTransactions.add(newTx);
    });
  }

  void _startAddNewTransaction(BuildContext ctx) {
    showModalBottomSheet(
      context: ctx,
      builder: (_) {
        return GestureDetector(
          onTap: () {},
          child: NewTransaction(_addNewTransaction),
          behavior: HitTestBehavior.opaque,
        );
      },
    );
  }

  void _deleteTransaction(String id) {
    setState(() {
      _userTransactions.removeWhere((tx) => tx.id == id);
    });
  }

  @override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);
    final isLandscape = mediaQuery.orientation == Orientation.landscape;
    final PreferredSizeWidget appbar = Platform.isIOS 

不能将类型为小部件"的值分配给类型为"PreferredSizeWidget"的变量.

A value of type 'Widget' can't be assigned to a variable of type 'PreferredSizeWidget'.

        ? CupertinoNavigationBar(
            middle: Text(
              'Personal Expenses ',
            ),
            trailing: Row(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                GestureDetector(
                  child: Icon(CupertinoIcons.add),
                  onTap: () => _startAddNewTransaction(context),
                )
              ],
            ),
          )
        : AppBar(
            title: Text(
              'Personal Expenses ',
              style: TextStyle(fontFamily: 'OpenSans'),
            ),
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.add),
                onPressed: () => _startAddNewTransaction(context),
              ),
            ],
          );
    final txListWidget = Container(
      height: (mediaQuery.size.height -
              appbar.preferredSize.height -
              mediaQuery.padding.top) *
          0.7,
      child: TransactionList(
        _userTransactions,
        _deleteTransaction,
      ),
    );
    final pageBody = SingleChildScrollView(
      child: Column(
        
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          if (isLandscape)
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('Show Chart'),
                Switch.adaptive(
                  activeColor: Theme.of(context).accentColor,
                  value: _showChart,
                  onChanged: (val) {
                    setState(() {
                      _showChart = val;
                    });
                  },
                ),
              ],
            ),
         
        ],
      ),
    );

返回Platform.isIOS?CupertinoPageScaffold(子代:pageBody,navigationBar:应用栏,

return Platform.isIOS ? CupertinoPageScaffold( child: pageBody, navigationBar: appbar,

不能将参数类型"PreferredSizeWidget"分配给参数类型"ObstructingPreferredSizeWidget?".

The argument type 'PreferredSizeWidget' can't be assigned to the parameter type 'ObstructingPreferredSizeWidget?'.

          )
        : Scaffold(
            .........,
                  ),
          );
  }
}

推荐答案

您的代码可在Flutter的非null安全版本中使用.我不知道为什么现在空安全版本会反对:

Your code works in non-null safe versions of Flutter. I do not know why the null safe version now objects to:

final PreferredSizeWidget appbar = Platform.isIOS ...

如果省略类型:

final appbar = Platform.isIOS ...

然后 appbar 被解释为 Widget ,并在 appbar.preferredSize 上给出错误.

Then appbar is interpreted as a Widget and gives an error on appbar.preferredSize.

您可以强制在运行时检查类型:

You can force the type to be examined at runtime:

final dynamic appbar = Platform.isIOS ...

这适用于我在Android设备上进行测试的情况.但是,我还没有在Apple设备上进行过测试.

This works for me testing with my Android device. However, I have not tested with an Apple device.

我创建了这个颤振问题,用于使用null安全的行为更改版本的颤动.您可以订阅该问题以获取更新通知.

I created this flutter issue for the change in behaviour with the null-safe version of flutter. You can subscribe to the issue to get update notifications.

修改:Flutter小组提出了一个 Dart问题,它解释了这种新行为,并建议使用:

The Flutter team raised a Dart issue which explains the new bahaviour and suggests using:

final PreferredSizeWidget appbar = (Platform.isIOS ? CupertinoNavigationBar() : AppBar()) 
                                   as PreferredSizeWidget;

这篇关于无法将类型为“小部件"的值分配给类型为"PreferredSizeWidget"的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆