before I start, I rarely use Dart or Flutter.
So, I have an application to read data through an API, and I have a class called Service_class.dart where I declared the Endpoint:
Future<ProductModel?> getSingleProductData() async {
ProductModel? result;
try {
var finalcode = "PRD89473320";
final response = await http.get(
Uri.parse(
"http://192.168.1.5/v1/api/product/read_single.php?productcode=$finalcode"),
headers: {
HttpHeaders.contentTypeHeader: "application/json",
},
);
if (response.statusCode == 200) {
final item = json.decode(response.body);
result = ProductModel.fromJson(item);
} else {
print("error");
}
} catch (e) {
log(e.toString());
}
return result;
}
At main.dart I have a Textfield:
TextField(
autofocus: true,
controller: _textController,
focusNode: _textNode,
decoration: InputDecoration(
border: OutlineInputBorder(),
),
),
Loading data (product_class.dart)
class DataClass extends ChangeNotifier {
ProductModel? product;
bool loading = false;
getProductData() async {
loading = true;
product = (await getSingleProductData())!;
loading = false;
notifyListeners();
}
}
Passing data to second screen (secondscreen.dart):
void initState() {
super.initState();
final productModel = Provider.of<DataClass>(context, listen: false);
productModel.getProductData();
}
And I would like to automatically fill out textfield value and send it to the $finalcode in Service_class.dart, then load and display on a second page.