Menu
Coddy logo textTech

Recap - Data Fetcher

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 81 of 110.

challenge icon

Challenge

Easy

Let's build a DataFetcher class that brings together all the async OOP concepts from this chapter! You'll create a data service that uses async initialization, async methods for fetching individual items, and a stream for delivering multiple results over time.

You'll organize your code into two files:

  • data_fetcher.dart: Create a DataFetcher class that simulates fetching data from a remote source. Your class should have:
    • A final String field called source that stores where the data comes from
    • A private constructor that initializes the source
    • A static async factory method connect(String sourceName) that returns a Future<DataFetcher>. Simulate a connection delay of 100 milliseconds, then return a new instance with the source set to the provided name
    • An async method fetchItem(int id) that returns a Future<String>. Simulate a 50 millisecond delay, then return a string in the format: [source]-Item-[id]
    • A stream generator method fetchMultiple(int count) that returns a Stream<String>. This should yield items for IDs from 1 up to and including count, using your fetchItem method for each one (await each fetch before yielding)
  • main.dart: Import your data fetcher and demonstrate all three async patterns working together:
    • Print Connecting to data source...
    • Use the async factory to connect to a source named CloudDB
    • Print Connected to [source] using the fetcher's source field
    • Print an empty line
    • Print Fetching single item:
    • Fetch and print the item with ID 42
    • Print an empty line
    • Print Fetching multiple items:
    • Use await for to iterate through fetchMultiple(3) and print each item
    • Print an empty line
    • Print All operations complete!

This challenge combines the static async factory pattern for initialization, regular async methods for single operations, and async* with yield for streaming multiple results. Notice how the stream method can call other async methods internally!

Expected output:

Connecting to data source...
Connected to CloudDB

Fetching single item:
CloudDB-Item-42

Fetching multiple items:
CloudDB-Item-1
CloudDB-Item-2
CloudDB-Item-3

All operations complete!

Try it yourself

import 'data_fetcher.dart';

void main() async {
  // TODO: Print 'Connecting to data source...'
  
  // TODO: Use the async factory to connect to a source named 'CloudDB'
  
  // TODO: Print 'Connected to [source]' using the fetcher's source field
  
  // TODO: Print an empty line
  
  // TODO: Print 'Fetching single item:'
  
  // TODO: Fetch and print the item with ID 42
  
  // TODO: Print an empty line
  
  // TODO: Print 'Fetching multiple items:'
  
  // TODO: Use 'await for' to iterate through fetchMultiple(3) and print each item
  
  // TODO: Print an empty line
  
  // TODO: Print 'All operations complete!'
}

All lessons in Object Oriented Programming