Home > Back-end >  flutter api returning data but its not workin in listviewbuilder
flutter api returning data but its not workin in listviewbuilder

Time:11-17

I'm tryig to solve an error in flutter list view builder once im calling my api functions its showing me the exact data i want, but when im giving the variable to listview builder it showing null value on that variable.

// import 'package:asanhisab/models/ledger_model.dart';
import 'package:asanhisab/utils/helper.dart';
import 'package:flutter/material.dart';
import 'package:odoo_api/odoo_api.dart';
import 'package:asanhisab/services/remoteservices.dart';

class GeneralLedgerScreen extends StatefulWidget {
  const GeneralLedgerScreen({Key? key}) : super(key: key);

  @override
  State<GeneralLedgerScreen> createState() => _GeneralLedgerScreenState();
}

class _GeneralLedgerScreenState extends State<GeneralLedgerScreen> {
  List? ledger;
  bool isloaded = false;
  @override
  void initState() {
    super.initState();
    getLedgerData();
  }

  getLedgerData() async {
    var ledger = await RemoteServices().getLedgerData();
    if (ledger != null) {
      setState(() {
        isloaded = true;
      });
    }
    logger.d(ledger.length);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('General Ledger'),
      ),
      body: Scrollbar(
        // child: Center(child: CircularProgressIndicator()),
        child: Center(
          child: Visibility(
            visible: isloaded,
            // ignore: sort_child_properties_last
            child: ListView.builder(
              itemCount: ledger?.length,
              itemBuilder: (context, index) {
                logger.d(ledger);
                return Card(
                  child: ListTile(
                    leading: const Icon(Icons.payment),
                    title: Text(index.toString()),
                    subtitle: const Text('Name'),
                    iconColor: Colors.blue,
                    isThreeLine: true,
                    trailing: const Text('1000'),
                    onTap: () {},
                  ),
                );
              },
            ),
            replacement: const CircularProgressIndicator(),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton.extended(
        label: const Text('Create'),
        icon: const Icon(Icons.add),
        backgroundColor: Colors.blue,
        onPressed: () {
          // Get.to(() => const CreateAccountHead());
        },
      ),
    );
  }
}

  getLedgerData() async {
    var client = OdooClient(storage.read('link'));
    var authres = await client.authenticate(storage.read('email'),
        storage.read('password'), storage.read('dbname'));
    if (authres.isSuccess) {
      var data = await client
          .callKW('ah_general_ledger.ah_account_head', 'search_read', []);
      if (data.getStatusCode() == 200) {
        return data.getResult();
      } else {
        logger.d('Error Occured while fetching data');
        // errorAlert(context, 'Error Occured while fetching data');
      }
    } else {
      logger.d('Error Occured');
      // errorAlert(context, 'Error Occured');
    }
    return null;
  }

I'm tryig to solve an error in flutter list view builder once im calling my api functions its showing me the exact data i want, but when im giving the variable to listview builder it showing null value on that variable.

CodePudding user response:

Try to print on the console before returning data.getResult(); @Arslan Mughal

CodePudding user response:

This is because your are redefined ledger in getLedgerData(), you should use your attribute instead.

getLedgerData() async {
    ledger = await RemoteServices().getLedgerData(); // remove var here at the beginning
    if (ledger != null) {
      setState(() {
        isloaded = true;
      });
    }
    logger.d(ledger.length);
  }

You can also use private field for your ledger and isloaded by adding _ caracter as prefix.

  • Related