I'm currently learning to write Mobile Applications in Flutter using Dart and recently I wanted to try to build an App where you can keep which movies you want to still watch and get some basic info about them. Now this is just for me, to train to understand the basic concepts of flutter and I wanted to ask how to make multiple widgets from one list. Ill show you my code and then elaborate more on it.
import 'package:flutter/material.dart';
class MovieList {
String name;
String url;
String description;
String actors;
int id;
MovieList(this.name, this.url, this.description, this.actors, this.id);
var movies = [
{
"21",
"assets/images/21_movie.jpeg",
"randomDescriptionfor21",
"idkwhichactors.1",
1
},
{
"Dirty Dancing",
"assets/images/dirty_dancing_movie.jpeg",
"randomDescriptionforDirtyDancing",
"idkwhichactors.2",
2
},
{
"Endless love",
"assets/images/endless_love_movie.jpeg",
"randomDescriptionforEndlesslove",
"idkwhichactors.3",
3
},
{
"Gut gegen Nordwind",
"assets/images/gut_gegen_nordwind_movie.jpeg",
"randomDescriptionforNordwind",
"idkwhichactors.4",
4
},
{
"Illuminati",
"assets/images/illuminati_movie.jpeg",
"randomDescriptionforIlluminati",
"idkwhichactors.5",
5
},
{
"Bridget Jones",
"assets/images/jones_movie.jpeg",
"randomDescriptionforBridgetJones",
"idkwhichactors.6",
6
},
{
"Kevin allein Zuhaus",
"assets/images/kevin_allein_zuhaus_movie.jpeg",
"randomDescriptionforKevin",
"idkwhichactors.7",
7
},
{
"Little Woman",
"assets/images/little_woman_movie.jpeg",
"randomDescriptionforLittleWoman",
"idkwhichactors.8",
8
},
{
"Liebe braucht keine Ferien",
"assets/images/love_movie.jpeg",
"randomDescriptionforLiebeundFerien",
"idkwhichactors.9",
9
},
{
"Marvel-Filme",
"assets/images/marvel_movie.jpeg",
"randomDescriptionforMarvel",
"idkwhichactors.10",
10
},
{
"Oceans 12",
"assets/images/oceans_12_movie.jpeg",
"randomDescriptionforOceans12",
"idkwhichactors.11",
11
},
{
"Pirates of the Carribean",
"assets/images/pirates_carribean_movie.jpeg",
"randomDescriptionforPiraten",
"idkwhichactors.12",
12
},
{
"Romeo und Julia",
"assets/images/romeo_and_julia_movie.jpeg",
"randomDescriptionforRomeoxJulia",
"idkwhichactors.13",
13
},
{
"A star is born",
"assets/images/star_movie.jpeg",
"randomDescriptionforStar",
"idkwhichactors.14",
14
},
{
"Die Entdeckung der Unendlichkeit",
"assets/images/stephen_hawking_movie.jpeg",
"randomDescriptionforStephen",
"idkwhichactors.15",
15
},
{
"Frühstück bei Tiffany",
"assets/images/tiffany_movie.jpeg",
"randomDescriptionforTIffany",
"idkwhichactors.16",
16
}
];
}
So as you see I define a class, MovieList, where I define certain variables (name, url, description, actors, id). Now under the initialisation of those Im defining a list with multiple movies, that have all of those attributes.
Question: How can use the list to create multiple Objects of MovieList and how could I implement a loop (or sth else idk how it would be done) to display multiple widgets, matching the movies I defined in the "movies" list?
Question: Is this even the right approach and would that even work? Like I said I'm pretty much starting with dart and flutter so Id really use your help.
Thanks in advance :)
So thanks a lot you already really helped me a lot. Now I wanted to do my Card Widget and that's basically how I changed up the code: 1:
``
import 'package:flutter/material.dart';
import './header.dart';
import './image_widget.dart';
import './movies_to_watch.dart';
var movies = [
{
'title': '21',
'bannerPath': 'assets/images/21_movie.jpeg',
'description': 'randomDescriptionforPirates',
'actors': [
'some actor',
]
},
{
'title': 'Dirty Dancing',
'bannerPath': 'assets/images/dirty_dancing_movie.jpeg',
'description': 'randomDescriptionforPirates',
'actors': [
'some actor',
]
},
{
'title': 'Endless Love',
'bannerPath': 'assets/images/endless_love_movie.jpeg',
'description': 'randomDescriptionforPirates',
'actors': [
'some actor',
]
},
{
'title': 'Gut gegen Nordwind',
'bannerPath': 'assets/images/gut_gegen_nordwind_movie.jpeg',
'description': 'randomDescriptionforPirates',
'actors': [
'some actor',
]
},
{
'title': 'Illuminati',
'bannerPath': 'assets/images/illuminati_movie.jpeg',
'description': 'randomDescriptionforPirates',
'actors': [
'some actor',
]
},
{
'title': 'Bridget Jones',
'bannerPath': 'assets/images/jones_movie.jpeg',
'description': 'randomDescriptionforPirates',
'actors': [
'some actor',
]
},
{
'title': 'Kevin allein zuhaus',
'bannerPath': 'assets/images/kevin_allein_zuhaus_movie.jpeg',
'description': 'randomDescriptionforPirates',
'actors': [
'some actor',
],
},
];
class Body extends StatelessWidget {
const Body({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(bottom: 20.0),
child: HeaderWidget(),
),
Padding(
padding: EdgeInsets.only(right: 20.0, left: 20.0),
child: FilmstoWatch(),
),
Padding(
padding: EdgeInsets.only(right: 20.0, left: 20.0),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
for (var movie in movies)
MovieWidget(model: MovieModel.fromJson(movie)),
],
),
),
),
],
),
);
}
}
``
and 2:
``
import 'package:flutter/material.dart';
class MyClass {
// here I am generating fake movies but you should use the data that you already have
List<Map<String, dynamic>> movies = List.generate(
15,
(index) => {
'title': 'fake title $index',
'actors': ['fake actor $index'],
'bannerPath': 'some/fake/path/$index',
'description': 'some fake description $index',
});
List<MovieModel> get models =>
movies.map((movie) => MovieModel.fromJson(movie)).toList();
List<Widget> get widgets =>
models.map((model) => MovieWidget(model: model)).toList();
}
class MovieModel {
MovieModel({
required this.title,
required this.actors,
required this.bannerPath,
required this.description,
});
factory MovieModel.fromJson(Map<String, dynamic> json) {
return MovieModel(
title: json['title'],
actors: json['actors'],
bannerPath: json['bannerPath'],
description: json['description'],
);
}
String title;
String bannerPath;
String description;
List<String> actors;
}
class MovieWidget extends StatelessWidget {
const MovieWidget(
{required this.model}); // if you decide to not make a model class, you would pass each value individually
final MovieModel model;
@override
Widget build(BuildContext context) {
// obviously this can be any widget you want
return SingleChildScrollView(
child: Card(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
elevation: 10.0,
margin: const EdgeInsets.all(10.0),
child: Column(
children: [
Text(model.title),
Container(
height: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
image: DecorationImage(
image: AssetImage(model.bannerPath),
),
),
),
Text(model.description),
if (model.actors.isNotEmpty) Text(model.actors.first),
],
),
),
);
}
}
``
If I run this I get this error thrown:
https://drive.google.com/file/d/1zbmsOq3lQIPUIZ92mvrg4sCcT21W8cc4/view?usp=sharing
when I replace the model.bannerPath with the actual string that should be inserted there it works. Thanks a lot for your help again!