'Is it possible to access a member variable of an object if it's in a list?
Assuming I have the following class named Quotes:
class Quotes {
String quote;
String author;
Quotes({required this.quote, required this.author});
}
And I have the following code on my main.dart
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<Quotes> quotes = [
Quotes(quote: 'Quote #1', author: 'John Doe'),
Quotes(quote: 'Quote #2', author: 'John Doe'),
Quotes(quote:'Quote #3', author: 'John Doe')
];
}
Let's say I wanna pass only the "quote" part of the object at index[0] to another variable of type String or anywhere else that would only accept a String as a parameter and not an object type. How would I be able to accomplish that?
Solution 1:[1]
The answer was very simple (Thank you jamesdlin)
you can access it by using: quotes[0].quote
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DumbAtMath |
