Home > Net >  Passing value from screen to screen then to API
Passing value from screen to screen then to API

Time:02-27

I'm trying to pass a variable from screen to screen and to pass it as a parameter in my API to make a delete request :

This is my Delete Page which is blank and there is nothing to show here . just a code to be executed when I navigate(press) on delete action.

import 'package:flutter/material.dart';
import 'package:form_field_validator/form_field_validator.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'HomePage.dart';

class ProductDelete extends StatefulWidget {


 int idToRemove=0;
// To avoid Nullable var 

ProductDelete(this.idToRemove, { Key? key}) : super(key: key);

  @override
  _ProductDeleteState createState() => _ProductDeleteState();
}

class _ProductDeleteState extends State<ProductDelete> {
      
 @override
 
  fetchUsers(int idToRemove) async {
    final response = await http.delete(
    Uri.parse('http://127.0.0.1:8000/api/product/$idToRemove'),
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
  );

And This is my product List and action button to load my Delete Screen with a parameter :

fetchUsers() async {
final response =
    await http.get(Uri.parse('http://127.0.0.1:8000/api/product'));

if (response.statusCode == 200) {
  var items = jsonDecode(response.body);
  setState(() {
    produits = items;
    print(produits[0]['productName']);
  });
} else {
  throw Exception('Error!');
}
}

ListTile(
          title: Text(produits[i]['productName']),
         trailing:  Row(
           mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                   Container(
                    child:  IconButton(
                             icon:  Icon(Icons.edit),
                             onPressed: () {
                                },           
                           ),
                  ),
                  Container(
                    child:  IconButton(
                             icon:  Icon(Icons.delete),
                             onPressed: () { 
                              Navigator.push(context,
                                  MaterialPageRoute(
                                      builder: (context) => ProductDelete( produits[i]['id'])));},
                           ),
                  )
                ],
              ),
        ),

CodePudding user response:

you can just call the fetchuser method on onpressed method you dont need to route to the product delete screen it will not call that method .

Container(
                child:  IconButton(
                         icon:  Icon(Icons.delete),
                         onPressed: () { 
                         fetchUsers(int idToRemove) async {
final response = await http.delete(
Uri.parse('http://127.0.0.1:8000/api/product/$idToRemove'),
headers: <String, String>{
  'Content-Type': 'application/json; charset=UTF-8',
},

); ),

  • Related