1

I'm currently stuck on the flutter development where I need to retrieve the index from map to other parts of the Scaffold. Is it possible to retrieve the index value from the map below into other parts of Scaffold (Floating Action Button)?

  Iterable<int> get positiveIntegers sync* {
    int i = articleList.length - 1;
    while (true) yield i--;
  }

  @override
  Widget build(BuildContext context) {
    int getLength = articleList.length;
    var list = positiveIntegers.take(getLength).toList();

    return Scaffold(
      appBar: AppBar(
        title: new Text(
          'Popular',
          textAlign: TextAlign.center,
        ),
        textTheme: TextTheme(
            title: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w700,
                color: Colors.black)),
        backgroundColor: Colors.white,
        centerTitle: true,
        elevation: 0.0,
      ),
      body: SwipeStack(
          children: list.map((index) {
              ...
          }
      ),
      floatingActionButton: Container(
        margin: EdgeInsets.only(top: 25, bottom: 25, left: 20),
        alignment: Alignment(0, 1),
        child: new Row(
          children: <Widget>[
            RawMaterialButton(
              onPressed: () {
                if (isOutOfCards == true) {
                  setState(() {
                    isOutOfCards = false;
                  });
                }
              },
              child: iconBuild(context),
              shape: new CircleBorder(),
              fillColor: Colors.red[700],
              splashColor: Colors.transparent,
              highlightColor: Colors.transparent,
              padding: const EdgeInsets.all(11),
            ),
            buttonTextBuild(context),
          ],
        ),
      ),
    );
  }

  Widget buttonTextBuild(BuildContext context) {
    if (isOutOfCards == false) {
      return Container(
          child: Text('$cardIndex/${articleList.length}',
              textAlign: TextAlign.left,
              style: TextStyle(
                  fontSize: 12.75,
                  fontWeight: FontWeight.w500,
                  letterSpacing: 1)));
    } else {
      return Container(
          child: Text('Start Again',
              textAlign: TextAlign.left,
              style: TextStyle(
                  fontSize: 12.75,
                  fontWeight: FontWeight.w500,
                  letterSpacing: 1)));
    }
  }

Like the code above I want to retrieve the 'index' from the list.map and pass it to the cardIndex inside buttonTextBuild.

1
  • 1
    Use List<int>.asMap(). Check the documentation here. Commented Mar 19, 2020 at 21:53

2 Answers 2

2

You can do this like that:

            children: [
              for(int index = 0; index < list.length; index++)
                Text(list[index])
            ]

Text widget is just example, you can use whatever widget you need or call function passing list[index] to get item

Sign up to request clarification or add additional context in comments.

2 Comments

How do i pass this into an int? Cuz its a string
list[index] is int, take it and pass to whatever you need
1

You can use the enumerate function from the quiver package:

children: enumerate(list).map((indexedValue) {
  var index = indexedValue.index;
  var value = indexedValue.value;
  ...
}),

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.