Quantcast
Channel: Active questions tagged listview - Stack Overflow
Viewing all articles
Browse latest Browse all 611

Return value from all list elements

$
0
0

Because I have no mock-data currently available, I created my own and I'm currently struggling to get specific values out of it.

The idea is to have a list of playlists, which I already instantiated, from where I get the data to display it in a ListView. That works already but I didn't find a solution on how to iterate through this list to get the names of playlist 1, name of playlist 2 and so on.

Currently I get a ListView returned with 2 Elements, but it returns only the value "one" - so the program doesn't iterate through the list. Because the other Element has the name "two".

[Here's a screenshot of the current status]enter image description here

Has anyone an idea how to fix it?

My Code:

Playlist-Class to create a specific playlist:

import 'package:flutter/material.dart';class Playlist {  Playlist({this.name,});  String? name;}

Mock-API to create a list of playlists and return it's name:

import 'package:myProject/stack/playlist.dart';import 'package:flutter/material.dart';class ApiDummyData {  static List<Playlist> playlistsList = [Playlist(name: "one"), Playlist(name: "two")];  int counter = -1;  dynamic getPlaylistName() {    counter++;    return playlistsList.elementAt(counter).name;  }}

My own created ListViewElement:

import 'package:myProject/api/api_dummy_data.dart';import 'package:myProject/stack/playlist.dart';import 'package:flutter/material.dart';class MyListViewElement extends StatelessWidget {  MyListViewElement({    super.key,  });  final getPlaylistNameInstance = ApiDummyData();  @override  Widget build(BuildContext context) {    return Container(        decoration: **...**,        child: Padding(          **...**,          child: Row(            children: [              Icon(                **...**              ),              **Text(getPlaylistNameInstance.getPlaylistName(),**                  style: TextStyle(color: Colors.white, fontSize: 25))            ],          ),        ));  }}

Using the data in the specific view:

class PlaylistView extends StatelessWidget {  const PlaylistView({super.key});  @override  Widget build(BuildContext context) {    return Scaffold(body: ListView(          children: [            for (int i = 0; i < ApiDummyData.playlistsList.length; i++)              buildChild(i)          ],        ),      );    }buildChild(int i) {    return InkWell(      onTap: () {        print("success");      },      child: MyListViewElement()),    );  }

I already tried to implement a counter or a for-loop but didnt fix the problem. :(I also tried to use the i counter from the view-file to put it in the child: MyListViewElement(i)), but there occured so many errors..


Viewing all articles
Browse latest Browse all 611

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>