Sushant Kumar
Sushant Kumar

Reputation: 509

A non-null String must be provided to a Text widget. 'package:flutter/src/widgets/text.dart': Failed assertion: line 360 pos 10: 'data != null'

Flutter Error : A non-null String must be provided to a Text widget. 'package:flutter/src/widgets/text.dart': Failed assertion: line 360 pos 10: 'data != null'

Another exception was thrown: A non-null String must be provided to a Text widget.

This is Main.dart

import 'package:bheekho_foundation/widgets/app.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(App());
}

This is App.dart

import 'package:bheekho_foundation/screens/home.dart';
import 'package:flutter/material.dart';

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

This is Home.dart

import 'dart:convert';    
import 'package:bheekho_foundation/models/request.dart';
import 'package:bheekho_foundation/services/request_service.dart';
import 'package:flutter/material.dart';


class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  RequestService _requestService = RequestService();

  // ignore: unused_element
  Future<List<Request>> _getAllRequest() async {
    var result = await _requestService.getAllRequests();
    List<Request> _list = List<Request>();
    if (result != null) {
      var requests = json.decode(result.body);
      requests.forEach((request) {
        var model = Request();
        model.user_id = request['user_id'];
        model.concern = request['concern'];
        model.message = request['message'];
        setState(() {
          _list.add(model);
        });
      });
    }
    return _list;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Bheekho App"),
        ),
        body: FutureBuilder<List<Request>>(
          future: _getAllRequest(),
          builder:
              (BuildContext context, AsyncSnapshot<List<Request>> snapshot) {
            if (snapshot.hasData) {
              return ListView.builder(
                  itemCount: snapshot.data.length,
                  itemBuilder: (context, index) {
                    return Column(
                      children: [
                        Card(
                          child: Text(snapshot.data[index].user_id),
                        ),
                      ],
                    );
                  });
            } else {
              return Container(
                child: Text('loading...'),
              );
            }
          },
        ));
  }
}

This is my Model request.dart

class Request {
  // ignore: non_constant_identifier_names
  int request_id;
  // ignore: non_constant_identifier_names
  String user_id;
  String concern;
  String message;
}

This is Request_Service.dart

import 'package:bheekho_foundation/repository/repository.dart';

class RequestService {
  Repository _repository;

  RequestService() {
    _repository = Repository();
  }

  getAllRequests() async {
    return await _repository.httpGet('users');
  }
}

This is Repository.dart

import 'package:http/http.dart' as http;

class Repository {
  String _baseUrl = "http://localhost:8000/api";

  httpGet(String api) async {
    return await http.get(_baseUrl + "/" + api);
  }
}

Erorr ScreenShot

Upvotes: 0

Views: 385

Answers (1)

Marijn Berends
Marijn Berends

Reputation: 203

Your snapshot.data[index].user_id is most likely null at this point. To make sure you do not assign null to a Text widget, you can change it to something like this:

Text(
    snapshot.data[index].user_id != null
        ? snapshot.data[index].user_id
        : ""
)

This actually checks if the data is null, when it's not, it loads the snapshot data. In case it is, it loads an empty string.

Upvotes: 1

Related Questions