1
resposta

Erro ao obter o name com o state

Boa tarde a todos!

Estou tendo um erro de compilação ao tentar obter o name através do state (final name = context.read()< NameCubit >().state;); segue abaixo o código:

class DashboardView extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    final name = context.read()<NameCubit>().state;

    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: Center(child: Text('Bytebank - Welcome $name')),
      ),
      body: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Image.asset('images/bytebank_logo.png'),
            ),
            Container(
              height: 120,
              child: ListView(
                scrollDirection: Axis.horizontal,
                children: [
                  _FeatureItem('Transfer', Icons.monetization_on,
                    onClick:() => _showContactsList(context) ,
                  ),

                  _FeatureItem('Transaction Feed',
                      Icons.description,
                      onClick:() => _showTransactionsList(context)
                  ),

                  _FeatureItem('Change Name', Icons.person_outline,
                    onClick:() => _showChangeName(context) ,
                  ),
                ],
              ),
            )
          ]),
    );
  }
}

Segue o erro:

======== Exception caught by widgets library =======================================================
The following assertion was thrown building DashboardView(dirty):
Tried to call Provider.of<dynamic>. This is likely a mistake and is therefore
unsupported.

If you want to expose a variable that can be anything, consider changing
`dynamic` to `Object` instead.
'package:provider/src/provider.dart':
Failed assertion: line 307 pos 7: 'T != dynamic'

The relevant error-causing widget was: 
  DashboardView file:///D:/Programacao/Estudos/Alura/Flutter/bytebank_bloc/lib/screens/dashboard.dart:13:14
When the exception was thrown, this was the stack: 
#2      Provider._inheritedElementOf (package:provider/src/provider.dart:307:7)
#3      Provider.of (package:provider/src/provider.dart:284:30)
#4      ReadContext.read (package:provider/src/provider.dart:610:21)
#5      DashboardView.build (package:bytebank/screens/dashboard.dart:22:26)
#6      StatelessElement.build (package:flutter/src/widgets/framework.dart:4569:28)
...
====================================================================================================

======== Exception caught by widgets library =======================================================
The following assertion was thrown building DashboardView(dirty):
Tried to call Provider.of<dynamic>. This is likely a mistake and is therefore
unsupported.

If you want to expose a variable that can be anything, consider changing
`dynamic` to `Object` instead.
'package:provider/src/provider.dart':
Failed assertion: line 307 pos 7: 'T != dynamic'

The relevant error-causing widget was: 
  DashboardView file:///D:/Programacao/Estudos/Alura/Flutter/bytebank_bloc/lib/screens/dashboard.dart:13:14
When the exception was thrown, this was the stack: 
#2      Provider._inheritedElementOf (package:provider/src/provider.dart:307:7)
#3      Provider.of (package:provider/src/provider.dart:284:30)
#4      ReadContext.read (package:provider/src/provider.dart:610:21)
#5      DashboardView.build (package:bytebank/screens/dashboard.dart:22:26)
#6      StatelessElement.build (package:flutter/src/widgets/framework.dart:4569:28)
...
====================================================================================================

Desde já agradeço a ajuda!

EDUARDO AGOSTINHO

1 resposta

Esta assim: final name = context.read()<NameCubit>().state;

Muda para: final name = context.read<NameCubit>().state;

Dentro do Build do seu DashboardView

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software