0

I am trying to create a search widget in flutter, for list I used the application list using device_apps (https://pub.dev/packages/device_apps). But Im getting an error "type 'Future' is not a subtype of type 'List?'" . To create the search widget I used help from this link.

Here is my code:

import 'dart:async';

import 'package:device_apps/device_apps.dart';
import 'package:flutter/material.dart';


class SearchWidget extends StatelessWidget {
  SearchWidget({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          TextField(onChanged: _filter),
          StreamBuilder<List<Application>>(
            initialData: lelist(),
            stream: _stream,
            builder:
                    (BuildContext context, AsyncSnapshot<List<Application>> snapshot) {
                  print(snapshot.data);
                  return ListView.builder(
                    shrinkWrap: true,
                    itemCount: snapshot.data.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Text(snapshot.data[index].appName);
                    },
                  );
                },
              )


        ],
      ),
    );
  }
}

StreamController<List<Application>> _streamController = StreamController<List<Application>>();
Stream<List<Application>> get _stream => _streamController.stream;
_filter(String searchQuery) {
  List<Application> _filteredList = lelist()
      .where((Application app) => app.appName.toLowerCase().contains(searchQuery.toLowerCase()))
      .toList();
  _streamController.sink.add(_filteredList);
}

lelist() async {
  List<Application> _dataFromQuerySnapShot = await DeviceApps.getInstalledApplications(includeAppIcons: true);
  _dataFromQuerySnapShot.sort((a, b) {
    return a.appName.toString().toLowerCase().compareTo(b.appName.toString().toLowerCase());
  });
  return _dataFromQuerySnapShot;
}



2 Answers 2

0

I think there it is unnecessary to load and sort the installed apps on every keystroke and therefore put all of this logic into initState. This also makes your _filter function synchronous.

import 'dart:async';

import 'package:device_apps/device_apps.dart';
import 'package:flutter/material.dart';

class SearchWidget extends StatefulWidget {
  SearchWidget({Key key}) : super(key: key);

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

class _SearchWidgetState extends State<SearchWidget> {
  final _streamController = StreamController<List<Application>>();
  List<Application> _dataFromQuerySnapShot;

  Stream<List<Application>> get _stream => _streamController.stream;

  @override
  void initState() {
    DeviceApps.getInstalledApplications(includeAppIcons: true)
        .then((List<Application> apps) {
      _dataFromQuerySnapShot = apps;
      _dataFromQuerySnapShot.sort((a, b) {
        return a.appName
            .toString()
            .toLowerCase()
            .compareTo(b.appName.toString().toLowerCase());
      });
      _streamController.sink.add(_dataFromQuerySnapShot);
    });
    super.initState();
  }

  @override
  void dispose() {
    _streamController.close();
    super.dispose();
  }

  _filter(String searchQuery) {
    final filteredApplications = _dataFromQuerySnapShot
        .where((Application app) =>
            app.appName.toLowerCase().contains(searchQuery.toLowerCase()))
        .toList();
    _streamController.sink.add(filteredApplications);
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          TextField(onChanged: _filter),
          StreamBuilder<List<Application>>(
            stream: _stream,
            builder: (BuildContext context,
                AsyncSnapshot<List<Application>> snapshot) {
              if (!snapshot.hasData) {
                return Center(child: CircularProgressIndicator());
              }
              return snapshot.data.isEmpty
                  ? Center(child: Text('Empty'))
                  : ListView.builder(
                      shrinkWrap: true,
                      itemCount: snapshot.data.length,
                      itemBuilder: (BuildContext context, int index) {
                        return Text(snapshot.data[index].appName);
                      },
                    );
            },
          )
        ],
      ),
    );
  }
}
4
  • I tried that but got the error: "The argument type 'Future<List<Application>>' can't be assigned to the parameter type 'List<Application>'"
    – rad10wave
    May 14, 2021 at 13:52
  • You cannot use lelist() as a value for your initialData as it is returning a Future. Either provide a different value for initialData or remove it and instead use something like if (!snapshot.hasData) { return CircularProgressIndicator() } while waiting for the first value from your stream.
    – hnnngwdlch
    May 14, 2021 at 15:55
  • I removed the initialData and used CircularProgressIndicator()...but now in print(snapshot.data) im getting I/flutter (26265): null and the Circular Indicator is rotating without showing any list...... When I entered words in the TextField I got another error in the console : Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'List<Application>'
    – rad10wave
    May 15, 2021 at 19:21
  • I tried using initialData: lelist() as List<Application>, but it is showing the same error : 'Future<dynamic>' is not a subtype of type 'List<Application>' in type cast
    – rad10wave
    May 15, 2021 at 19:26
0

I think you need to add asList after calling your future.

lelist() async {
 List<Application> _dataFromQuerySnapShot = await 
   DeviceApps.getInstalledApplications(includeAppIcons: true);
    _dataFromQuerySnapShot.sort((a, b) {
    return 
    a.appName.toString().toLowerCase().compareTo(b.appName.toString().toLowerCase());
 }) as List;
 return _dataFromQuerySnapShot;

}

1
  • No, still got the same error even after using as List.
    – rad10wave
    May 14, 2021 at 13:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.