简体   繁体   English

如何通过未来<void> _name(String _string) 到 Flutter 中的小部件?

[英]How to pass Future<void> _name(String _string) to a widget in Flutter?

This is code for my class...这是我班级的代码...

class List extends StatefulWidget {
@override
_ListState createState() => _ListState();
}

class _ListState extends State<List> {
  Future<void> _makePhoneCall(String url) async {
   if (await canLaunch(url)) {
    await launch(url);
    } else {
      throw 'Could not launch $url';
    }
}

@override
Widget build(BuildContext context) {
return Scaffold(
    appBar: customAppbar('Contact Us'),
    body: ListView(children: <Widget>[
      _listTile('1234-1234567-123', 'Exam Enquiry', Icons.call,
          _makePhoneCall('tel:12345'))
    ]));
}
}

And I want to pass _makePhoneCall Function to a Widget _listTile but got an error:我想将 _makePhoneCall 函数传递给小部件 _listTile 但出现错误:

The argument type 'Future' can't be assigned to the parameter type 'Future Function(String)'参数类型 'Future' 不能分配给参数类型 'Future Function(String)'

Code for _listTile widget is follow: _listTile 小部件的代码如下:

Widget _listTile(String title, String subTitle, IconData iconData,
Future<void> _makePhoneCall(String url)) {
return Padding(
padding: EdgeInsets.only(left: 24.0),
child: ListTile(
  title: Text(title),
  subtitle: Text(subTitle),
  trailing: Padding(
    padding: const EdgeInsets.all(0.0),
    child: RawMaterialButton(
      onPressed: () => _makePhoneCall,
      child: Icon(
        iconData,
        color: Colors.white,
      ),
      elevation: 2,
      shape: CircleBorder(),
      fillColor: Color(0xff7061aa),
    ),
  ),
),
);

} }

Here are the necessary changes:以下是必要的更改:

    body: ListView(children: <Widget>[
      _listTile('1234-1234567-123', 'Exam Enquiry', Icons.call,
          _makePhoneCall)
    ]));
    child: RawMaterialButton(
      onPressed: () => _makePhoneCall('tel:12345'),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Future 传递变量<string>在 Flutter 中无效?</string> - How can I pass variable from Future<String> to void in Flutter? 如何在 flutter 中使用 Future 字符串初始化 widget 变量的 state - How to init state of widget variable with Future string in flutter Flutter - 如何在不执行的情况下将未来传递给小部件? - Flutter - How to pass a future to a widget without executing it? 我想传递一个小部件,它也在 Flutter 的 void 方法中传递一个字符串值 - I want to pass a widget which also pass a String value in a void method in Flutter Flutter _TypeError(输入“未来<string> ' 不是类型 'Future' 的子类型<widget> ?')</widget></string> - Flutter _TypeError (type 'Future<String>' is not a subtype of type 'Future<Widget>?') 如何在 flutter 中缓存未来的字符串 - How to cache a future string in flutter 如何从未来获取字符串<String>在颤振中? - How to get a String from a Future<String> in Flutter? Flutter:如何转换未来<string>到字符串?</string> - Flutter : How To Convert Future<String> to String? 如何从未来返回一个字符串<string>在 Flutter</string> - How to return a string from a Future<String> in Flutter 如何分配未来<string>到 flutter 中的字符串变量</string> - How to assign future<String> to a String variable in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM