Asked  2 Months ago    Answers:  5   Viewed   402 times

I am using a plugin for flutter called search_widget. The data parameter of this widget takes a list. But as I use sqlite for fetching data, I have it in Future<List> form. Is there any way I can convert Future<List> to List? Or any other way to get this working.

 Answers

64

Borrowing the example from search_widget you need dataList in a widget like this:

SearchWidget<LeaderBoard>(
   dataList: list,
   textFieldBuilder: (TextEditingController controller, FocusNode focusNode) {
     return MyTextField(controller, focusNode);
   },
 )

Sure, you can convert Future<List> into List like other answers suggest. But you won't be able to do dataList: await _sqliteCall(); because build methods are designed to be pure and sychronous.

While the Future completes you will have to return something like a progress indicator. For that you can use a FutureBuilder:

FutureBuilder<List<Leaderboard>>(
  future: _sqliteCall(),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      return SearchWidget<LeaderBoard>(
        dataList: snapshot.data,
        textFieldBuilder: (TextEditingController controller, FocusNode focusNode) {
          return MyTextField(controller, focusNode);
        },
      )
    }
    return CircularProgressIndicator();
  }
),

Of course this can also be done with a StatefulWidget, you can check this article for a detailed explanation of the issue.

Wednesday, August 11, 2021
 
Ewanziak
answered 2 Months ago
79

It depends on what the integer is supposed to encode. You could convert the date to a number of milliseconds from some previous time. People often do this affixed to 12:00 am January 1 1970, or 1900, etc., and measure time as an integer number of milliseconds from that point. The datetime module (or others like it) will have functions that do this for you: for example, you can use int(datetime.datetime.utcnow().timestamp()).

If you want to semantically encode the year, month, and day, one way to do it is to multiply those components by order-of-magnitude values large enough to juxtapose them within the integer digits:

2012-06-13 --> 20120613 = 10,000 * (2012) + 100 * (6) + 1*(13)

def to_integer(dt_time):
    return 10000*dt_time.year + 100*dt_time.month + dt_time.day

E.g.

In [1]: import datetime

In [2]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:def to_integer(dt_time):
:    return 10000*dt_time.year + 100*dt_time.month + dt_time.day
:    # Or take the appropriate chars from a string date representation.
:--

In [3]: to_integer(datetime.date(2012, 6, 13))
Out[3]: 20120613

If you also want minutes and seconds, then just include further orders of magnitude as needed to display the digits.

I've encountered this second method very often in legacy systems, especially systems that pull date-based data out of legacy SQL databases.

It is very bad. You end up writing a lot of hacky code for aligning dates, computing month or day offsets as they would appear in the integer format (e.g. resetting the month back to 1 as you pass December, then incrementing the year value), and boiler plate for converting to and from the integer format all over.

Unless such a convention lives in a deep, low-level, and thoroughly tested section of the API you're working on, such that everyone who ever consumes the data really can count on this integer representation and all of its helper functions, then you end up with lots of people re-writing basic date-handling routines all over the place.

It's generally much better to leave the value in a date context, like datetime.date, for as long as you possibly can, so that the operations upon it are expressed in a natural, date-based context, and not some lone developer's personal hack into an integer.

Friday, July 2, 2021
 
juananrey
answered 4 Months ago
32

Here you go! showDialog takes a WidgetBuilder as a parameter so you can return any widget.

   import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(home: new MyApp()));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            showDialog(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    content: Stack(
                      overflow: Overflow.visible,
                      children: <Widget>[
                        Positioned(
                          right: -40.0,
                          top: -40.0,
                          child: InkResponse(
                            onTap: () {
                              Navigator.of(context).pop();
                            },
                            child: CircleAvatar(
                              child: Icon(Icons.close),
                              backgroundColor: Colors.red,
                            ),
                          ),
                        ),
                        Form(
                          key: _formKey,
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              Padding(
                                padding: EdgeInsets.all(8.0),
                                child: TextFormField(),
                              ),
                              Padding(
                                padding: EdgeInsets.all(8.0),
                                child: TextFormField(),
                              ),
                              Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: RaisedButton(
                                  child: Text("Submitß"),
                                  onPressed: () {
                                    if (_formKey.currentState.validate()) {
                                      _formKey.currentState.save();
                                    }
                                  },
                                ),
                              )
                            ],
                          ),
                        ),
                      ],
                    ),
                  );
                });
          },
          child: Text("Open Popup"),
        ),
      ),
    );
  }
}

Hop it helps!

Sunday, August 8, 2021
 
eliotlencelot
answered 2 Months ago
73

You might want to try removing the app from the device and reinstalling it. Depending on how you're launching, it might not be writing over the old install file.

Tuesday, August 10, 2021
 
Xaisoft
answered 2 Months ago
70

Your string looks like a valid JSON, so this should work for you:

import 'dart:convert';
...

var a = '["one", "two", "three", "four"]';
var ab = json.decode(a);
print(ab[0]); // returns "one"
Thursday, August 12, 2021
 
Bram
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :