How do I use the value of a variable in other places?

Asked

Viewed 189 times

-1

I’m getting a value from a variable that is passed through a route, but I want to take that value and put it into another code block.

File 1

final String nomeCategoria;

File 2

Stream<List<CategoriaModel>> getCategorias() {
    return firestore.collection($nomeCategoria).snapshots().map((query) {
      return query.documents.map((doc) {
        return CategoriaModel.fromDocuments(doc);
      }).toList();
    });
  }

but it doesn’t work

  • How are you passing the parameter through your route? Why are you using $nomeCategoria?? Do not skip the initial study steps... EDITE your question with more information.

  • Excuse me, indicate some material for study ?

  • The documentation itself is a good for this, start by the link quoted in the answer below.

1 answer

1


What you need from what I understand is to send a date to another screen. You can do it this way:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class Todo {
  final String title;
  final String description;

  Todo(this.title, this.description);
}

void main() {
  runApp(MaterialApp(
    title: 'Passing Data',
    home: TodosScreen(
      todos: List.generate(
        20,
        (i) => Todo(
          'Todo $i',
          'A description of what needs to be done for Todo $i',
        ),
      ),
    ),
  ));
}

class TodosScreen extends StatelessWidget {
  final List<Todo> todos;

  TodosScreen({Key key, @required this.todos}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Todos'),
      ),
      body: ListView.builder(
        itemCount: todos.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(todos[index].title),
            // When a user taps the ListTile, navigate to the DetailScreen.
            // Notice that you're not only creating a DetailScreen, you're
            // also passing the current todo through to it.
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => DetailScreen(),
                  // Pass the arguments as part of the RouteSettings. The
                  // DetailScreen reads the arguments from these settings.
                  settings: RouteSettings(
                    arguments: todos[index],
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }
}

class DetailScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final Todo todo = ModalRoute.of(context).settings.arguments;

    // Use the Todo to create the UI.
    return Scaffold(
      appBar: AppBar(
        title: Text(todo.title),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Text(todo.description),
      ),
    );
  }
}

Source: https://flutter.dev/docs/cookbook/navigation/passing-data

Browser other questions tagged

You are not signed in. Login or sign up in order to post.