Flutter Exception Error Handling Google Apps Script generated json

蹲街弑〆低调 提交于 2020-06-28 04:01:07

问题


Developing my first Flutter mobile app, a code snippet to fetch a json from:

'https://my-json-server.typicode.com/typicode/demo/posts' 

...successfully responds, decodes, parses, etc. Then when i test with doc uploaded to git as:

'https://raw.githubusercontent.com/rays-github/theirmenu/master/db.json'

...this also works. But when I try to use my own data (Google Web Apps Script publishing a Google Sheets spreadsheet as json):

'https://script.googleusercontent.com/macros/echo?user_content_key=_DZABYr6b6k274bCyLNtzSBd1jtYF_WpuFDYAtNQT-uE6uj0teMefPEiNDxNisIH0ew63RSj757Xh5smCcvouuLLk_VcYyB8m5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnPKmEGJr49ifP_3P8Fcnrtzcwn0zyFgFMfS_we8kf_vIvupeaUN7ec2K60MRzRqUBQ&lib=MNDmyszRDOPMr7WJ3Tg4jKCcl7uh4ZtSK'

...I get errors:

Exception has occurred.
FormatException (FormatException: Unexpected character (at line 2, character 1)
<!DOCTYPE html>
^
)

Here is my Flutter snippet:

import 'dart:convert';
import 'package:theirmenu001pt00/tm_menuitem_model.dart';
import 'package:http/http.dart';

class HttpService {
  // final String postsUrl = "https://my-json-server.typicode.com/typicode/demo/posts";
  // final String postsUrl = "https://raw.githubusercontent.com/rays-github/theirmenu/master/db.json";
  final String postsUrl = "https://script.googleusercontent.com/macros/echo?user_content_key=7zmRRkd__iPae6VZ9oq5TTNjfEm3QQV9EYBvQN-awvPS4-HNw2C4wbUSC8ud0J9rfFuxXvwhWPMjiJj5GUVQvGHDvinAYraCm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnPKmEGJr49ifP_3P8Fcnrtzcwn0zyFgFMfS_we8kf_vIvupeaUN7ec2K60MRzRqUBQ&lib=MNDmyszRDOPMr7WJ3Tg4jKCcl7uh4ZtSK";

  Future<List<Post>> getMenuItems() async {
    Response res = await get(postsUrl);

    if (res.statusCode == 200) {
      List<dynamic> body = jsonDecode(res.body);

      List<Post> posts =
          body.map((dynamic item) => Post.fromJson(item)).toList();

      return posts;
    } else {
      throw "Can't get posts.";
    }
  }
}

Here is my Google Web App Script:

function doGet(e){

 // Sheet url
 var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1jsBS-RBNRxYU66WFkJHvrzHLGmNqxBzzQfaHJO6i6UY/edit#gid=446843772");

// Sheet Name
 var sheet = ss.getSheetByName("Users");
  
 return getUsers(sheet); 
  
}


function getUsers(sheet){
  var jo = {};
  var dataArray = [];

// collecting data from 2nd Row , 1st column to last row and last column
  var rows = sheet.getRange(2,1,sheet.getLastRow()-1, sheet.getLastColumn()).getValues();
  
  for(var i = 0, l= rows.length; i<l ; i++){
    var dataRow = rows[i];
    var record = {};
    record['userId'] = dataRow[0];
    record['id'] = dataRow[1];
    record['title'] = dataRow[2];
    record['body'] = dataRow[3];
    
    dataArray.push(record);
    
  }  
  
  jo = dataArray;
  
  var result = JSON.stringify(jo);
  
  return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);
  
}  
 

SCREENSHOT - MICROSOFT VISUAL STUDIO CODE RUN:

SCREENSHOT - UNAUTHENTICATED REQUEST VIA BROWSER: Please, advise. Any help is greatly appreciated. thanks!

来源:https://stackoverflow.com/questions/62486506/flutter-exception-error-handling-google-apps-script-generated-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!