前提
Flutterを用いて家計簿アプリを作っています。
Radioボタンを実装中に以下の問題が発生しました。
長文で申し訳ございませんが、ご確認いただきたく存じます。
実現したいこと
・Radioボタンで支払方法の選択が出来るように実装したい。
・Radioボタンを横並びで表示したい。
発生している問題・エラーメッセージ
エラーメッセージ The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?'. ### 該当のソースコード ```Dart ソースコード ```import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: '支出', theme: ThemeData( primarySwatch: Colors.orange, ), home: const MyHomePage(), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key}) : super(key: key); @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { String _payment = '未選択'; void _handleRadioButton(String payment) => setState(() { _payment = payment; }); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( leading: IconButton( icon: Icon( Icons.arrow_back, color: Colors.black, ), onPressed: () => Navigator.of(context).pop(), ), title: Text( '支出', style: TextStyle(fontSize: 25), ), centerTitle: true, ), body: Center( child: Column(children: <Widget>[ Container( width: 300, decoration: BoxDecoration( border: Border( bottom: BorderSide(), )), margin: EdgeInsets.all(20), child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[ Center( child: Text( 'お支払方法:' + _payment, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), ), Row( children:<Widget>[ Radio( activeColor: Colors.orange, value: '現金', groupValue: _payment, onChanged: _handleRadioButton, ), Text('現金'), ], ), Row( children:<Widget>[ Radio( activeColor: Colors.orange, value: 'クレジットカード', groupValue: _payment, onChanged: _handleRadioButton, ), Text('クレジットカード'), ], ), ], ), ), ]), )); } } ### 試したこと ・onChangedの値をvalueに変更 ・MyHomePageStateクラスの_handleRadioButtonをWidget内に移動 void _handleRadioButton(String payment) => setState(() { _payment = payment; } の部分でvoidが上手く反映されていないのかな、と考え 引数を変えてみたり、そもそものRadioボタンの書き方を変えてみたりしましたが、 一向にvoidが割り当てられず、エラーが止まりませんでした。 勉強不足で申し訳ございませんが、ご教示いただけますと幸いです。 どうぞよろしくお願いいたします。 ※以下参考URL ・ https://www.yururiwork.net/archives/776 ・ https://www.choge-blog.com/programming/flutterradiobutton-horizontal/ ・ https://www.choge-blog.com/programming/flutterlistview-radiobutton/

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/11/09 09:42