flutterdartcalllog

How do I pass data from method _callLogs to body and print it?


I'm new to using flutter framework and I want to pass call logs to my app so I can print them. How do I do that?

class _QuoteListState extends State<QuoteList> {

   Future<bool>_callLogs() async {
     Iterable<CallLogEntry> entries = await CallLog.get();

   }

 @override
 Widget build(BuildContext context) {
   return Scaffold(
     backgroundColor: Colors.grey[200],
     appBar: AppBar(
       title: Text('Calls Log'),
       centerTitle: true,
       backgroundColor: Colors.purple,
     ),
     body: ListView.builder(
       itemCount: entries.length,
       itemBuilder: (context, index){

       }
     )
   );

Solution

  • you have to fetch data from async function and set to local variables so it is good to call setState inside the _callLogs as and make your widget stateful and write as below

       import 'package:call_log/call_log.dart';
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MaterialApp(
        home: LogsList(),
      ));
    }
    
    class LogsList extends StatefulWidget {
      @override
      _LogsListState createState() => _LogsListState();
    }
    
    class _LogsListState extends State<LogsList> {
      @override
      void initState() {
        super.initState();
        _callLogs();
      }
    
      Future<void> _callLogs() async {
        Iterable<CallLogEntry> entries = await CallLog.get();
        List _data = entries.map((data) => data).toList();
        setState(() {
          _entries = _data;
        });
      }
    
      List<CallLogEntry> _entries = [];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.grey[200],
          appBar: AppBar(
            title: Text('Calls Log'),
            centerTitle: true,
            backgroundColor: Colors.purple,
          ),
          body: ListView.builder(
              itemCount: _entries.length,
              itemBuilder: (context, i) {
                final CallLogEntry callLogEntry = _entries[i];
    
                return ListTile(
                  title: Text(callLogEntry.formattedNumber),
                  subtitle: Text(callLogEntry.duration.toString()),
                );
              }),
        );
      }
    }
    

    make sure to add this to your project Android Manifest: