In the following codes, I have presented a minimal code (however I could not reproduce the issue in the minimal code). The MyCustomInputWidget
provides two callback functions: one for the retrieval of input data, and other for the retrieval of computed result in a list. The values, both input and result are made available to the callback methods once the user clicks the ElevatedButton
. In the main.dart
, both input and result are expected to be received using callback method. And, in this minimal code, I am able to retrieve them without any problem. But, in my actual code, which I can not publish here, only the input is retrieved and result list is not. I have checked that the list is correctly assigned values and is non-empty during the assignment as widget.getResultStringList(myNumberServer.myOutput());
but in the callback method in main.dart
, the list is shown empty in actual code. I, however, also figured out an alternative way which is by sharing the data using the provider
.
So, I want to know if the method to retrieve data and result as presented below is not ideal way? If not, its shortcomings. Is sharing the input and result using provider
the right way to go?
Codes:
main.dart:
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String userInputValue = "";
List<String> userResultList = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("example")),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text("hello world"),
MyCustomInputWidget(
getInputNumberString: (value) {
userInputValue = value;
},
getResultStringList: (value) {
userResultList = value;
},
),
Consumer<MyCustomProvider>(
builder: (context, myProvider, _) {
if (myProvider.isResultAvailableForUse) {
return Text(
"Input: ${userInputValue} ; Result: ${userResultList}",
);
} else {
return Text("No result available.");
}
},
),
],
),
);
}
}
my_custom_input_widget.dart:
class MyNumberServer {
final int value;
List<String> _myList = [];
MyNumberServer({required this.value});
List<String> myOutput() {
switch (value) {
case 0:
_myList = [];
break;
case 1:
_myList = ["one"];
break;
case 2:
_myList = ["one", "two"];
break;
default:
_myList = ["one", "two", "three", "four"];
}
return _myList;
}
}
class MyCustomInputWidget extends StatefulWidget {
final Function(String inputNumberString) getInputNumberString;
final Function(List<String> resultString) getResultStringList;
const MyCustomInputWidget({
super.key,
required this.getInputNumberString,
required this.getResultStringList,
});
@override
State<MyCustomInputWidget> createState() => _MyCustomInputWidgetState();
}
class _MyCustomInputWidgetState extends State<MyCustomInputWidget> {
late TextEditingController controllerData;
late MyCustomProvider mcpObj;
@override
void initState() {
super.initState();
controllerData = TextEditingController();
mcpObj = Provider.of<MyCustomProvider>(context, listen: false);
}
@override
void dispose() {
controllerData.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: controllerData,
decoration: InputDecoration(labelText: "Input Number (0-9)"),
),
ElevatedButton(
onPressed: () {
widget.getInputNumberString(controllerData.text);
MyNumberServer myNumberServer = MyNumberServer(
value: int.tryParse(controllerData.text) ?? 1,
);
widget.getResultStringList(myNumberServer.myOutput());
mcpObj.setResultAvailableForUseStatus();
},
child: Text("Process Data"),
),
],
);
}
}
my_custom_provider.dart:
class MyCustomProvider with ChangeNotifier {
bool isResultAvailableForUse = false;
void setResultAvailableForUseStatus() {
isResultAvailableForUse = true;
notifyListeners();
}
}
UPDATE: I implemented counters that increment within the getInputNumberString
and getResultStringList
callback methods within the main.dart file, and display the counts in the page. In my actual app, the counter for both seem to increment, meaning the callback gets executed but only the input value (which in actual code is an object reference) gets updated. Seeing this, in addition to passing the lists as input to the other callback function, I also passed another object reference to the same encapsulated lists, and this time, only the object got updated but not the lists in the callback method in main.dart. So, I'm confused if there's a problem with getting lists from callback method? Or something I am missing?
UPDATE 02: I am finding it really strange that passing list variables to callback function is actually passing empty list for some reason, despite the printing the list variables in debug console show that the passed list variables actually contain values. Instead, of passing list variable, I tried passing ["item01", "item02"]
and it works for such lists passed directly. Also, the list variables get passed correctly for the first time the callback is executed, from next time onward, the list variables don't seem to get assigned in the callback in my actual code. What could be going wrong? Please help.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744857982a4597522.html
评论列表(0条)